Given an unsigned number, of type 'int' or 'long', this function returns a corresponding number of the same type, with its byte order reversed. So if input = 0xABCDEF, then output = 0xEFCDAB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
def ReverseByteOrder(data): """ Method to reverse the byte order of a given unsigned data value Input: data: data value whose byte order needs to be swap data can only be as big as 4 bytes Output: revD: data value with its byte order reversed """ s = "Error: Only 'unsigned' data of type 'int' or 'long' is allowed" if not ((type(data) == int)or(type(data) == long)): s1 = "Error: Invalid data type: %s" % type(data) print(''.join([s,'\n',s1])) return data if(data < 0): s2 = "Error: Data is signed. Value is less than 0" print(''.join([s,'\n',s2])) return data seq = ["0x"] while(data > 0): d = data & 0xFF # extract the least significant(LS) byte seq.append('%02x'%d)# convert to appropriate string, append to sequence data >>= 8 # push next higher byte to LS position revD = int(''.join(seq),16) return revD
This type of a function is sometimes needed to work with machines with different 'endian'ness that what python supports. Its also sometimes needed to decode data coming back or to be sent to embedded microcontroller based systems.
There can be atleast a couple more ways to implment the same thing: a) using 'byteswap' method from the "array" module: It only becomes a little cumbersome to parse the data given and then create an array of that type. the array module requires a 'typecode' which varies based on the number of bytes in the data being put into the array. The array module is not as intuitive as the idea of an "array" is in itself.
b) there is a suggestion about using the 'struct' module. I am unclear on the use of the 'struct' module itself. the documentation does not show well...how things are being done with the 'struct' module. I am guessing that to do the above, we'd have to pack the given number into a struct. which will return a string. That string then needs to be cut into 2 char worth parts and put into a list. Then we'd call list.reverse(), join the list into a string, and then unpack the same using struct module again. If there's another approach by using struct, please do comment.
I still love the simplicity of my approach in the recipe above.