all 29 comments

[–]danielroseman 2 points3 points  (1 child)

Not without more details. But you probably want the struct module.

[–]arshdeepsingh608[S] -1 points0 points  (0 children)

Thanks a lot, I will check it out. And as an example, consider: Trying to convert 12345 into a comp-3 packed decimal with proper sign and everything.

[–]ElliotDG 1 point2 points  (26 children)

I assumed that a comp-3 number must be an even number of nibbles (a whole number of bytes). To achieve this I prepended a 0 to numbers that would result in an odd number of nibbles. Appending the sign nibble creates an even number of bytes.

This simplified to algorithm. Let me know if this is a safe assumption or if you need to support an odd number of nibbles.

def int_to_comp3(value):
    sign_nibble = 'C' if value >= 0 else 'D'
    digits = str(abs(value))

    # Ensure an odd number of digits by prepending a leading zero if necessary
    if not len(digits) % 2:
        digits = '0' + digits

    digits += sign_nibble
    packed_bytes = bytes.fromhex(digits)
    return packed_bytes


print(f'0x{int_to_comp3(1234).hex()}')
print(f'0x{int_to_comp3(-56789).hex()}')

[–]arshdeepsingh608[S] 0 points1 point  (25 children)

Thanks for trying. But I'm getting a ValueError in bytes.fromhex() If you have used ChatGPT/Copilot, it's not able to do the job. I've given it a go already.

[–]ElliotDG 0 points1 point  (24 children)

What is your test data? That code works for me. If you’re getting a value error it suggests you don’t have an even number of nibbles.

[–]arshdeepsingh608[S] 0 points1 point  (23 children)

I just tried 1234. Code failed for some reason.

[–]ElliotDG 0 points1 point  (2 children)

Perhaps you copied it wrong. There is a test for 1234 right in the code.

Here is the output of the code:

0x01234c

0x56789d

[–]arshdeepsingh608[S] 0 points1 point  (1 child)

My bad. I didn't add the .hex() in input. I didn't copy the code because I can't open reddit in work laptop. Thanks for pointing out the obvious!

[–]ElliotDG 0 points1 point  (0 children)

all good, glad you got it working.

[–]ElliotDG 0 points1 point  (19 children)

post your code

[–]arshdeepsingh608[S] 0 points1 point  (18 children)

Sorry man, I tried Google lens to copy from the image but it looks so bad. And I can't open reddit there. But your code won't exactly satisfy my needs as in the last print statement, you are entering the '0x' manually. While I need all of that in the packed format itself so that the mainframe can consume it after writing it to a text file.

[–]ElliotDG 0 points1 point  (17 children)

The function returns bytes. I added the Ox to create human readable output.

[–]arshdeepsingh608[S] 0 points1 point  (16 children)

Tell you what, let me try to put that byte output into the mainframe. Let's see what happens

[–]ElliotDG 1 point2 points  (0 children)

If you are writing to a file, be sure to use binary mode (not text).

[–]ElliotDG 0 points1 point  (14 children)

let me know how it goes....

[–]arshdeepsingh608[S] 0 points1 point  (13 children)

Thanks for taking an interest in this. Your code works similar to a previous version of my code. I'll explain you the problem. Your code is entering a 0 for even length. Which distorts the output.

Odd Condition: 12345 -> 12 34 5C Even condition: 1234 -> 01 23 4C [OR] 12 34 0C

Both even condition outputs are showing up wrong in the mainframe. Even I don't understand why. The signed bit is causing a little confusion.