1

I want to get the value of 99997 in big endian which is (2642804992) and then return the answer as a long value

here is my code in python:

v = 99997
ttm = pack('>i', v) # change the integer to big endian form 
print ("%x"), {ttm}
r = long(ttm, 16) # convert to long (ERROR)
return r

Output: %x set(['\x00\x01\x86\x9d'])

Error: invalid literal for long() with base 16: '\x00\x01\x86\x9d'

As the string is already in hex form why isn't it converting to a long? How would I remove this error and what is the solution to this problem.

1
  • What do you expect the value of r to be? Commented Aug 10, 2016 at 7:19

4 Answers 4

1

pack will return a string representation of the data you provide.

The string representation is different than a base 16 of a long number. Notice the \x before each number.

Edit:

try this

ttm = pack('>I',v)
final, = unpack('<I',ttm)
print ttm

Notice the use of I, this so the number is treated as an unsigned value

Sign up to request clarification or add additional context in comments.

2 Comments

so how would i convert this string of bytes into a long value?
you can use struct.unpack, just like @uphill suggested. You can change the formatting, use '<i' and this will turn into a conversion from bigendian to little endian
1

You have to use struct.unpack as a reverse operation to struct.pack.

r, = unpack('<i', ttm)

this will r set to -1652162304.

Comments

0

You just converted the integer value to big endian binary bytes.

This is useful mostly to embed in messages addressed to big-endian machines (PowerPC, M68K,...)

Converting to long like this means parsing the ttm string which should be 0x1869D as ASCII. (and the print statement does not work either BTW)

If I just follow your question title: "Convert hexadecimal string to long":

just use long("0x1869D",16). No need to serialize it. (BTW long only works in python 2. In python 3, you would have to use int since all numbers are represented in the long form)

Well, I'm answering to explain why it's bound to fail, but I'll edit my answer when I really know what you want to do.

7 Comments

That's weird: the answers are downvoted when they're explaining something useful and the question is upvoted (well, not anymore :)) when it's not clear!
Yeah, not sure why the downvotes. Both current answers seem correct to me; upvoting. Couldn't have been the OP, since he doesn't have enough rep to downvote.
@Andrew Cheong: yes, not the OP! Sometimes we should refrain from answering such unclear questions, even if we know why the OP is struggling with the different concepts. That's a like a rep trap. thanks BTW
True, but also those new to the community don't necessarily know how to ask clear questions. In any case, it wasn't the OP who downvoted (in case that was your implication).
aaaah it's clearer now: then please update your post because 1) there are 3 answers that don't really answer your question and 2) I'll be able to remove my downvote (and maybe my answer too). Only Radu Dita got what you wanted. You should accept it, end of story :)
|
0

This is a nice question.

Here is what you are looking for.

s = str(ttm)
for ch in r"\bx'":
    s = s.replace(ch, '')

print(int(s, 16))

The problem is that ttm is similar to a string in some aspects. This is what is looks like: b'\x00\x01\x86\x9d'. Supplying it to int (or long) keeps all the non-hex characters. I removed them and then it worked.

After removing the non-hex-digit chars, you are left with 0001869d which is indeed 99997

Comment I tried it on Python 3. But on Python 2 it will be almost the same, you won't have the b attached to the string, but otherwise it's the same thing.

1 Comment

i tried s = str(ttm) but it gave an arror that utf-8 cannot encode convert a byte 0x9d.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.