473,394 Members | 1,800 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Convert a little-endian hex string to a number

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
5 29121
bartonc
6,596 Expert 4TB
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
6,596 Expert 4TB
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
6,596 Expert 4TB
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
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
6,596 Expert 4TB
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

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

Similar topics

1
by: Sorisio, Chris | last post by:
Ladies and gentlemen, I've imported some data from a MySQL database into a Python dictionary. I'm attempting to tidy up the date fields, but I'm receiving a 'mx.DateTime.Error: cannot convert...
9
by: Richard W Collens | last post by:
Hi Folks, Just wondering if there is any real need to convert an application from 2000 or XP to 2003, if the application is not going to grow or change. Ive had a couple people ask about it...
0
by: NoSpamForMe | last post by:
I am currently a java programmer, with some knowledge of the C# language but very little knowledge about ASP.NET, and I am know wondering if someone can *translate* the java servlet code below to...
3
by: Ignacio Marcos | last post by:
Hi all. I need to convert files on my server from gif to jpg format. I tried with the thumb function, but it does not work (on the line thumb.Save(Response.OutputStream,...
37
by: James Radke | last post by:
Hello, I found some code that I could use in my application on the web, but it is written in C#, and I would like to convert it to VB. And I am having problems with one section. Is there...
14
by: John Smith | last post by:
Can someone convert from C# into VB this line for me: if (c is System.Web.UI.HtmlControls.HtmlForm)
8
by: Franck | last post by:
Hi, I'm lookin for an external tool which could convert any type of documents to pdf from server. (with as much functionnalities as possible : schedule, pooling, multi thread, embedding font,...
6
by: tombsy | last post by:
Hello Group. I work for a company who is about to embark on a long awaited Office upgrade from Office 97 to Office XP. Office XP comes with Access 2002. I am an accomplished Access developer...
4
by: meyousikmann | last post by:
I am working on an implementation of the Longest Common Subsequence problem (as I understand it, this problem can be used in spell checking type activities) and have used this site to understand...
4
by: =?Utf-8?B?S3VlaXNoaW9uZyBUdQ==?= | last post by:
I have a VC# .NET project which is imported from an OCX control using AxImp. I would like to convert it into a VC++ .NET project. Is there any way to do it and how to do it?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.