Reputation: 3751
The following statement is from a documentation I'm following.
“7c bd 9c 91” 2442968444(919cbd7c hex)usec = 2442.9sec
If you assume:
Then its easy to see how they got 919cbd7c
simply by flipping it abcd
to dcba
.
What I don't understand is why they aren't filliping the actual bits.
That is to say I expect 19c9dbc7
rather than 919cbd7c
.
Is there a way to convert the original string to what they expect?
EG: convert 7cbd9c91
to 919cbd7c
?
I know that I can split the string in twos and reverse the order. But is there a way python is aware of this and can decode it automatically?
Here is the documentation. The part in question is on the 2nd line of page 22.
Upvotes: 0
Views: 1291
Reputation: 104832
I think you're trying to put too much thought into it. The hex pairs you're seeing are actually single bytes, and the order of the bits within the bytes is unambiguous. It's only the byte-order of the higher-level multi-byte integer that can go more than one way. Fortunately, byte-order swapping is very easy, since computers have to do it all the time (network byte order is big-endian, but most PCs these days are little-endian internally).
In Python, just pass the raw bytestring you're getting (which would be b"\x7c\xbd\x9c\x91"
for the example data shown in the documentation) to struct.unpack
with an appropriate format parameter. Since the documentation says it's a little endian 4-byte number, use "<L"
as the format code to specify a "little-endian unsigned long integer":
>>> bytestring = b"\x7c\xbd\x9c\x91" # from wherever
>>> struct.unpack("<L", bytestring)
(2442968444,)
Upvotes: 2