468,720 Members | 1,554 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,720 developers. It's quick & easy.

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

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

Jul 18 '05 #1
5 5680
Manish Jethani wrote:
Alexander Eisenhuth wrote:

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

Maybe I don't understand your question...

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" ?????

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

-Manish

Sorry ...

This two lines converts 0.5 to a string in binary
representation
from struct import *
bin_str = pack('!f', 0.5)


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

Jul 18 '05 #2
Manish Jethani wrote:
Alexander Eisenhuth wrote:

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

Maybe I don't understand your question...

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" ?????

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

-Manish

Sorry ...

This two lines converts 0.5 to a string in binary
representation
from struct import *
bin_str = pack('!f', 0.5)


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
Jul 18 '05 #3
Alexander Eisenhuth wrote:
This two lines converts 0.5 to a string in binary
representation
>>>from struct import *
from binascii import hexlify
>>>bin_str = pack('!f', 0.5)


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.

--
Manish Jethani (manish.j at gmx.net)
phone (work) +91-80-51073488

Jul 18 '05 #4
On Fri, 25 Jul 2003 14:57:57 +0200, Alexander Eisenhuth <st****@stacom-software.de> wrote:
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
Jul 18 '05 #5
FWIW here's an interesting way to get at the bit:
if 13 is the number you want to represent in binary and you want 6 bits:
[13>>x & 1 for x in range(6,-1,-1)]

[0, 0, 0, 1, 1, 0, 1]

Bob Gailer
bg*****@alum.rpi.edu
303 442 2625
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.500 / Virus Database: 298 - Release Date: 7/10/2003

Jul 18 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

21 posts views Thread by Batista, Facundo | last post: by
1 post views Thread by Ferran Foz | last post: by
1 post views Thread by tony.fountaine | last post: by
4 posts views Thread by vcnewbie | last post: by
4 posts views Thread by dondigitech | last post: by
9 posts views Thread by bryonone | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.