1

I am having string of four hex numbers like:

"0x00 0x01 0x13 0x00" 

which is equivalent to 0x00011300. How i can convert this hex value to integer?

1

3 Answers 3

3

Since you are having string of hex values. You may remove ' 0x' from the string to get the single hex number string. For example:

my_str =  "0x00 0x01 0x13 0x00"
hex_str = my_str.replace(' 0x', '')

where value hold by hex_str will be:

'0x00011300'   # `hex` string

Now you may convert hex string to int as:

>>> int(hex_str, 16)
70400
Sign up to request clarification or add additional context in comments.

Comments

1
>>> s="0x00 0x01 0x13 0x00"
>>> a=0
>>> for b in s.split():
...    a = a*256 + int(b,16)
... 
>>> a
70400
>>> hex(a)
'0x11300'

Comments

0

The answer is here Convert hex string to int in Python.

Without the 0x prefix, you need to specify the base explicitly, otherwise there's no way to tell:

x = int("deadbeef", 16)

With the 0x prefix, Python can distinguish hex and decimal automatically.

print int("0xdeadbeef", 0)
3735928559

print int("10", 0)
10

(You must specify 0 as the base in order to invoke this prefix-guessing behavior; omitting the second parameter means to assume base-10. See the comments for more details.)

Comments

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.