0

I have data structure defined as follows:

class Factory_Params_Get_Command(Structure):
_pack_ = 1
_fields_ = [("SN",c_byte * 32),
            ("Voltage",c_byte),
            ("Reserved",c_byte * 30)]
# Print the fields
def __str__(self):
    return "Serial Number: %s"  % (list(self.SN))

This prints the serial number like:

[0, 32, 58, 73.....]

I would like to print the serial number as set of hexadecimal values, where each byte is represented by 2 hex numbers, and , if possible , without commas and without spaces. Something like this:

03C8A0D6.....

Would appreciate your help

1
  • Have you tried hex(number) function? Commented May 4, 2015 at 12:43

1 Answer 1

1

Possibly something like:

hexstring = ''.join('%02X' % b for b in self.SN)

That applies the formatting string %02X to every byte in the array, and then concatenates everything into a single string. For example:

>>> import ctypes
>>> sn = (ctypes.c_byte*32)(*range(1,32))
>>> hexstring = ''.join('%02X' % b for b in sn)
>>> print hexstring
0102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F00
>>> 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.