How to print c_type byte array as set of hexadecimal values in python?
Question: 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


Answer: 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
    >>> 
    

