By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,345 Members | 1,783 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,345 IT Pros & Developers. It's quick & easy.

Convert a little-endian hex string to a number

P: 3
I'd like to convert a little endian hex string:

Expand|Select|Wrap|Line Numbers
  1. '00020000020000FC'
to a number.

I was going to use
Expand|Select|Wrap|Line Numbers
  1. struct.unpack('>q', theString)
but this is meant for a string where the bytes in the string correspond to bytes in the number.

I think that something like the opposite of the hex() function would work perfect as long as it supported little-endian conversion.

The file I'm reading is an ASCII file that looks like this:

010FA3456FF
A4F5ABB3DE
...

That is the actual ASCII text there. It's really a waste of space to store data this way but that's what I have to read.

Any ideas?
May 25 '07 #1
Share this Question
Share on Google+
5 Replies


bartonc
Expert 5K+
P: 6,596
I'd like to convert a little endian hex string:

Expand|Select|Wrap|Line Numbers
  1. '00020000020000FC'
to a number.

I was going to use
Expand|Select|Wrap|Line Numbers
  1. struct.unpack('>q', theString)
but this is meant for a string where the bytes in the string correspond to bytes in the number.

I think that something like the opposite of the hex() function would work perfect as long as it supported little-endian conversion.

The file I'm reading is an ASCII file that looks like this:

010FA3456FF
A4F5ABB3DE
...

That is the actual ASCII text there. It's really a waste of space to store data this way but that's what I have to read.

Any ideas?
There is support for differing byte arrangements in the ctypes module, but I haven't looked into that yet. Off the top of my head:
We know that this is not what you are looking for:
>>> int('00020000020000FC', 16)
562949986975996L

But perhaps something like this could work for you:
>>> aLittleEndianHex = '00020000020000FC'
>>> aBigEndianList = []
>>> for i in range(0, len(aLittleEndianHex), 2):
... aBigEndianList.insert(0, aLittleEndianHex[i:i+2])
...
>>> aBigEndianList
['FC', '00', '00', '02', '00', '00', '02', '00']
>>>
Now you can use struct or what ever you want to.
May 25 '07 #2

bartonc
Expert 5K+
P: 6,596
By the way, welcome to the Python Forum on TheScripts. Thank you for your input on the matlab question. I like your style!
I hope you stick around,
Barton
May 25 '07 #3

bartonc
Expert 5K+
P: 6,596
There is support for differing byte arrangements in the ctypes module, but I haven't looked into that yet. Off the top of my head:
We know that this is not what you are looking for:
>>> int('00020000020000FC', 16)
562949986975996L

But perhaps something like this could work for you:
>>> aLittleEndianHex = '00020000020000FC'
>>> aBigEndianList = []
>>> for i in range(0, len(aLittleEndianHex), 2):
... aBigEndianList.insert(0, aLittleEndianHex[i:i+2])
...
>>> aBigEndianList
['FC', '00', '00', '02', '00', '00', '02', '00']
>>>
Now you can use struct or what ever you want to.
perhaps a string?

>>> aBigEndianStr = ""
>>> for byteStr in aBigEndianList:
... aBigEndianStr += byteStr
...
>>> aBigEndianStr
'FC00000200000200'
>>>
May 25 '07 #4

P: 3
Thanks guys! I used both ideas except that I concatenated the list of strings using S.join(sequence)

Here's the function I came up with:

Expand|Select|Wrap|Line Numbers
  1.     def hexStrEndianSwap(self, theString):
  2.         """Rearranges character-couples in a little endian hex string to
  3.         convert it into a big endian hex string and vice-versa. i.e. 'A3F2'
  4.         is converted to 'F2A3'
  5.  
  6.         @param theString: The string to swap character-couples in
  7.         @return: A hex string with swapped character-couples. -1 on error."""
  8.  
  9.         # We can't swap character couples in a string that has an odd number
  10.         # of characters.
  11.         if len(theString)%2 != 0:
  12.             return -1
  13.  
  14.         # Swap the couples
  15.         swapList = []
  16.         for i in range(0, len(theString), 2):
  17.             swapList.insert(0, theString[i:i+2])
  18.  
  19.         # Combine everything into one string. Don't use a delimeter.
  20.         return ''.join(swapList)
  21.  
May 25 '07 #5

bartonc
Expert 5K+
P: 6,596
Thanks guys! I used both ideas except that I concatenated the list of strings using S.join(sequence)

Here's the function I came up with:

Expand|Select|Wrap|Line Numbers
  1.     def hexStrEndianSwap(self, theString):
  2.         """Rearranges character-couples in a little endian hex string to
  3.         convert it into a big endian hex string and vice-versa. i.e. 'A3F2'
  4.         is converted to 'F2A3'
  5.  
  6.         @param theString: The string to swap character-couples in
  7.         @return: A hex string with swapped character-couples. -1 on error."""
  8.  
  9.         # We can't swap character couples in a string that has an odd number
  10.         # of characters.
  11.         if len(theString)%2 != 0:
  12.             return -1
  13.  
  14.         # Swap the couples
  15.         swapList = []
  16.         for i in range(0, len(theString), 2):
  17.             swapList.insert(0, theString[i:i+2])
  18.  
  19.         # Combine everything into one string. Don't use a delimeter.
  20.         return ''.join(swapList)
  21.  
"".join() - I don't know how I missed that one. One of the sweetest tricks in the book.

Thanks for the update. It really helps others when you post back with your solutions. Keep posting,
Barton
May 26 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.