0

I trying to convert numbers from decimal to hex. How do I convert float values to hex or char in Python 2.4.3?

I would then like to be able to print it as ("\xa5\x (new hex number here)"). How do I do that?

ire_and_curses
  • 68,372
  • 23
  • 116
  • 141
user349845
  • 11
  • 1
  • 1
  • Related: http://stackoverflow.com/questions/1592158/python-convert-hex-to-float – miku May 25 '10 at 11:47
  • 2
    Do you want the binary representation of the IEEE float in hex, do you just want the integer portion in hex, or do you want the whole thing in hex? – Gabe May 25 '10 at 11:48
  • 1
    Hi, what's your use for this? Are you trying to keep decimal precision if you print/read the number? You might want to look at decimal floating point for this, it's a lot more portable and you won't end up with an angry mob at your gate with torches and pitchforks. – Matt Curtis May 25 '10 at 12:21

3 Answers3

1

From python 2.6.5 docs in hex(x) definition:

To obtain a hexadecimal string representation for a float, use the float.hex() method.

joaquin
  • 82,968
  • 29
  • 138
  • 152
  • would you mind please to give an example of its use? I am trying to convert this 0.554 to hex by using float.hex(value)? and how can I write it as (\x30\x30\x35\x35)? – user349845 May 25 '10 at 13:20
  • In this case you could have written it as 0.554.hex(). But the result would have been wildly different than what you're expecting. see http://docs.python.org/library/stdtypes.html#float.hex – Jeremy Brown May 25 '10 at 14:56
  • I realize that what the OP actually wants is the Hex values for the ASCII codes of the numbers. I am not editing my answer as Jeremy already gave a solution below. In any case the hint given by jordan 0.554 == '\x30\x30\x35\x35' was also wrong as the decimal point was not indicated and actually '\x30\x30\x35\x35' == '0055'. – joaquin May 25 '10 at 15:04
1

Judging from this comment:

would you mind please to give an example of its use? I am trying to convert this 0.554 to hex by using float.hex(value)? and how can I write it as (\x30\x30\x35\x35)? – jordan2010 1 hour ago

what you really want is a hexadecimal representation of the ASCII codes of those numerical characters rather than an actual float represented in hex.

"5" = 53(base 10) = 0x35 (base 16)

You can use ord() to get the ASCII code for each character like this:

>>> [ ord(char) for char in "0.554" ]
[48, 46, 53, 53, 52]

Do you want a human-readable representation? hex() will give you one but it is not in the same format that you asked for:

>>> [ hex(ord(char)) for char in "0.554" ]
['0x30', '0x2e', '0x35', '0x35', '0x34']
#   0      .        5       5       4

Instead you can use string substitution and appropriate formatters

res = "".join( [ "\\x%02X" % ord(char) for char in "0.554" ] )
>>> print res
\x30\x2E\x35\x35\x34

But if you want to serialize the data, look into using the struct module to pack the data into buffers.

edited to answer jordan2010's second comment

Here's a quick addition to pad the number with leading zeroes.

>>> padded_integer_str = "%04d" % 5
>>> print padded_integer_str
0005
>>> res = "".join( [ "\\x%02X" % ord(char) for char in padded_integer_str] )
>>> print res
\x30\x30\x30\x35

See http://docs.python.org/library/stdtypes.html#string-formatting for an explanation on string formatters

Jeremy Brown
  • 17,880
  • 4
  • 35
  • 28
0

You can't convert a float directly to hex. You need to convert to int first.

hex(int(value))

Note that int always rounds down, so you might want to do the rounding explicitly before converting to int:

hex(int(round(value)))
Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • Yes, understood, but how can I do this; I converted the hex to decimal. However, my problem is to convert this float decimal numbers to hex in python For example: The hexadecimal for 0.26 is \x30\x32\x36\x36, how this can be done in Python? – user349845 May 25 '10 at 12:07
  • What do you mean, converted it to decimal? Can you give an example of what you've done? Representing decimal numbers can be done a bunch of different ways, so it would be helpful if you could describe it in some detail (or provide some code). – Matt Curtis May 25 '10 at 12:28