16
votes

I have integer number in ex. 16 and i am trying to convert this number to a hex number. I tried to achieve this by using hex function but whenever you provide a integer number to the hex function it returns string representation of hex number,

my_number = 16
hex_no = hex(my_number)    
print type(hex_no) // It will print type of hex_no as str.

Can someone please tell me how to convert hex number in string format to simply a hex number.
Thanks!!

8
How does this matter? You need the hex representation only for printing. A number is a number; decimal, binary and hex are just representations to the outside world. - Tim Pietzcker
Why does the title say "string to number" when "number to hex string" is really what you want? - poke
Strange. print hex_no should NOT print quotes. Can you check that again? - Manoj Govindan
It doesn't always represent hex as string, it's a meaningless question. There's no such thing as a a hex integer or a decimal integer - it's all binary inside the computer in any case. When you convert an integer into its hexadecimal string representation (which was what hex does) then of course you get a string... - The Archetypal Paul
there's no such thing as a "hexadecimal number". There are integers (which don't have any radix) and the string representations of integers (which can be hex, decimal, binary or something else). You can't do arithmetic directly on strings. So it's not a "workaround" to convert them to the integer, it's the correct way of doing it. - The Archetypal Paul

8 Answers

18
votes
4
votes

Sample Code :

print "%x"%int("2a",16)
3
votes

Are you asking how to convert the string format hexadecimal value '16' into an integer (that is, end up with an integer with decimal value 22)? It's not clear from your question. If so, you probably want int('16', 16)

2
votes

Using the string formatters (new first, then old):

>>> '{:x}'.format( 12345678 )
'bc614e'

>>> '%x' % ( 12345678 )
'bc614e'
1
votes

Your code works for me, no apostrophes added.

Python 2.6.4 (r264:75708, Oct 26 2009, 08:23:19) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> my_number = 16
>>> hex_no = hex(my_number)
>>> print hex_no
0x10
>>> _

Note, by the way, that there's no such thing as a "hex number". Hex is just a way to specify a number value. In the computer's memory that number value is usually represented in binary, no matter how it's specified in your source code (decimal, hex, whatever).

Cheers & hth.,

– Alf

0
votes

With Python 2.6.5 on MS Windows Vista, the command line interpreter behaves this way:

>>>hex(16)
'0x10'
>>>print hex(16)
0x10

I guess this is the normal behavior:

>>>'abc'
'abc'
>>>print 'abc'
abc

I hope it helps

0
votes

I think here most of the answers were misinterpreted or they understood the question wrongly.

To answer to your question it is IMPOSSIBLE to convert resultant string representation of Hex data to Hex numbers(integer representation).

Because, when you convert an integer to hex by doing following

>>> a = hex(34)
>>> print type(a)
<type 'str'>
>>> print a
0x22
>>> a
'0x22'

And some answers were confused here,

>>> print a
0x22
>>> a
'0x22'

When you type print a in interpreter it will result the string data WITHOUT quotes and If you simply type the variable name without using print statement then it would print the string data WITH single/double quotes.

Though the resultant value is Hex data but the representation is in STRING.

As per Python docs you cannot convert to Hex number as I told earlier.

Thanks.

-2
votes
def add_hex2(hex1, hex2):
    """add two hexadecimal string values and return as such"""
    return hex(int(hex1, 16) + int(hex2, 16))
print add_hex2('0xff', '0xff')  # 0x1fe