data conversion question (binary string to 'real string')

  • Thread starter Alexander Eisenhuth
  • Start date
A

Alexander Eisenhuth

Hallo all there,

maby I don't see the forest because of all that trees, but :

from struct import *


# how can I convert
f = float(0.5)

# with
bin_str = pack('!f', 0.5)

# now bin_str is '?\x00\x00\x00'


# to "3F000000" ?????


Thanks a lot for any help
 
A

Alexander Eisenhuth

Manish said:
Alexander Eisenhuth wrote:




Maybe I don't understand your question...




???????????????
What is your question?

-Manish
Sorry ...

This two lines converts 0.5 to a string in binary
representation

now bin_str is '?\x00\x00\x00', wich is the representation
of 0.5 in the memory. I need now to convert this binary
string to "3F000000", where '3F' is the hex value of the '?'
in ascii - table.

Any ideas ??

Thaks
Alexander
 
A

Alexander Eisenhuth

Manish said:
Alexander Eisenhuth wrote:




Maybe I don't understand your question...




???????????????
What is your question?

-Manish
Sorry ...

This two lines converts 0.5 to a string in binary
representation

now bin_str is '?\x00\x00\x00', wich is the representation
of 0.5 in the memory. I need now to convert this binary
string to "3F000000", where '3F' is the hex value of the '?'
in ascii - table.

Any ideas ??

Thaks
Alexander
 
M

Manish Jethani

Alexander said:
This two lines converts 0.5 to a string in binary
representation

from binascii import hexlify
now bin_str is '?\x00\x00\x00', wich is the representation
of 0.5 in the memory. I need now to convert this binary
string to "3F000000", where '3F' is the hex value of the '?'
in ascii - table.

Any ideas ??

foo = hexlify(bin_str)
print foo

-Manish

PS: You posted the same message thrice, by mistake.
 
B

Bengt Richter

Hallo all there,

maby I don't see the forest because of all that trees, but :

from struct import *


# how can I convert
f = float(0.5)

# with
bin_str = pack('!f', 0.5)

# now bin_str is '?\x00\x00\x00'


# to "3F000000" ?????
That leading '?' is the character representation of a string byte whose ordinal value
is 3F hex, followed by three characters represented by \x00 (whose ordinal
values are zero). I.e., your binary bytes are being represented as characters in a string,
and when it's shown interactively you see the values as a sequence of character glyphs
(or \x.. hex escape representations if no glyphs are available), which is the normal way
to display a string.

If you want the *hex string* representation of that string-represented byte sequence, you can
convert each byte-as-character first to its ordinal value and then the ordinal value to a
2-char hex string representation, and then join those all into a single string,
e.g.,, showing the character transformations:
>>> [c for c in struct.pack('!f', 0.5)] ['?', '\x00', '\x00', '\x00']
>>> [ord(c) for c in struct.pack('!f', 0.5)] [63, 0, 0, 0]
>>> ['%02X'%ord(c) for c in struct.pack('!f', 0.5)]
['3F', '00', '00', '00']

And finally, all you need is
>>> ''.join(['%02X'%ord(c) for c in struct.pack('!f', 0.5)])
'3F000000'

Now you have a hex string representation of the network-ordered (big-endian-ordered)
bytes of a single precision (32-bit) float.

Regards,
Bengt Richter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top