I'd like to convert a little endian hex string:
to a number.
I was going to use - 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?
5 29341 bartonc 6,596
Recognized Expert Expert
I'd like to convert a little endian hex string:
to a number.
I was going to use - 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('0002000002 0000FC', 16)
562949986975996 L
But perhaps something like this could work for you:
>>> aLittleEndianHe x = '00020000020000 FC'
>>> aBigEndianList = []
>>> for i in range(0, len(aLittleEndi anHex), 2):
... aBigEndianList. insert(0, aLittleEndianHe x[i:i+2])
...
>>> aBigEndianList
['FC', '00', '00', '02', '00', '00', '02', '00']
>>>
Now you can use struct or what ever you want to.
bartonc 6,596
Recognized Expert Expert
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
bartonc 6,596
Recognized Expert Expert
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('0002000002 0000FC', 16)
562949986975996 L
But perhaps something like this could work for you:
>>> aLittleEndianHe x = '00020000020000 FC'
>>> aBigEndianList = []
>>> for i in range(0, len(aLittleEndi anHex), 2):
... aBigEndianList. insert(0, aLittleEndianHe x[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
'FC000002000002 00'
>>>
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: -
def hexStrEndianSwap(self, theString):
-
"""Rearranges character-couples in a little endian hex string to
-
convert it into a big endian hex string and vice-versa. i.e. 'A3F2'
-
is converted to 'F2A3'
-
-
@param theString: The string to swap character-couples in
-
@return: A hex string with swapped character-couples. -1 on error."""
-
-
# We can't swap character couples in a string that has an odd number
-
# of characters.
-
if len(theString)%2 != 0:
-
return -1
-
-
# Swap the couples
-
swapList = []
-
for i in range(0, len(theString), 2):
-
swapList.insert(0, theString[i:i+2])
-
-
# Combine everything into one string. Don't use a delimeter.
-
return ''.join(swapList)
-
bartonc 6,596
Recognized Expert Expert
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: -
def hexStrEndianSwap(self, theString):
-
"""Rearranges character-couples in a little endian hex string to
-
convert it into a big endian hex string and vice-versa. i.e. 'A3F2'
-
is converted to 'F2A3'
-
-
@param theString: The string to swap character-couples in
-
@return: A hex string with swapped character-couples. -1 on error."""
-
-
# We can't swap character couples in a string that has an odd number
-
# of characters.
-
if len(theString)%2 != 0:
-
return -1
-
-
# Swap the couples
-
swapList = []
-
for i in range(0, len(theString), 2):
-
swapList.insert(0, theString[i:i+2])
-
-
# Combine everything into one string. Don't use a delimeter.
-
return ''.join(swapList)
-
"".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
Sign in to post your reply or Sign up for a free account.
Similar topics |
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 value to a time value' error. It's
related to glibc returning an error to a pre-1970 date, I think.
My question: /how/ do I go through the Python direction I've created to
remove the pre-1970 date objects? Ideally, I would be able to iterate...
|
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 (converting to 2003) but I can't see a reason
they should other than their IT manager want's to buy new stuff.
Any help or opinion would be great
Thanks!
|
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
ASP.NET/C# ?
Please note that I am not asking about the best possible way of doing
the same thing with ASP.NET architecture with those "code behind"
things I have heard a little about... but what I am asking for is a
*translation* (other...
|
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,
Imaging.ImageFormat.Jpeg)). Anybody can help me, tanks a
lot.
|
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 anyone that could tell me how to
convert these two things?
1)
| |
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)
|
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, encrypt, merge, ...)
(Actually testing neevia Document Convert pro)
Does anyone got experience and/or links regarding this ?
|
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 so I have read up on the complexities
of upgrade in the corporate environment.
Also the need to carry out a full MDB audit to get rid of the redundancy etc
etc
|
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 the
problem and its solution:
http://en.wikibooks.org/wiki/Algorithm_implementation/Strings/Longest_common_subsequence
For those that understand algorithms and can talk Python, I want to
convert the Python code in the section "Reading out all...
|
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?
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |