6 Byte Data to Integer

Hi,
I have a 6-bytes of data 01 32 B0 99 A3 2A that I need to convert into a number value.

Using an unpack it only accepts either 4-bytes or 8-bytes for a number.

I need to convert 01 32 B0 99 A3 2A so i get 1317222851370

Thanks
yoeddyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

farzanjCommented:
Try this
>>> a= '01 32 B0 99 A3 2A'
>>> int(a.replace(" ", ""),16)
1317222851370
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
gelonidaCommented:
Depending on the endianness you can prepend or append two zero bytes and then do your unpack.

0
gelonidaCommented:
A small example for little endian
TWO_ZERO = '\x00\x00' # constant to prepend / append

istr = 'ABCDEF'  # string to decode

val = struct.unpack('q',istr+ TWO_ZERO)
print '%012x' % val

Open in new window

0
gelonidaCommented:
I am confused.

Did you want to convert six bytes or did you want to convert a string with 12 hex digits and some spaces?

My first example was for little endian.
It seems you were looking for big endian above the corrected example.


TWO_ZERO = '\x00\x00' # constant to prepend / append

istr = ='\x01\x32\xB0\x99\xA3\x2A'  # string of 6 bytes

val = struct.unpack('>q', TWO_ZERO + istr)
print val

Open in new window

0
gelonidaCommented:
converting arbitrary length strings into a big endian integer could be done with attached code
(though there might be more efficient solutions)

Please note, that I made a typo in my previous post. (line 3 has one '=' character too much)


istr = '\x01\x32\xB0\x99\xA3\x2A' # or string of any other length
int(''.join([ '%02x' % ord(v) for v in istr ]), 16)

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Python

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.