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

Discussion in 'Python' started by Alexander Eisenhuth, Jul 25, 2003.

  1. 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
    Alexander Eisenhuth, Jul 25, 2003
    #1
    1. Advertising

  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
    Alexander Eisenhuth, Jul 25, 2003
    #2
    1. Advertising

  3. 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
    Alexander Eisenhuth, Jul 25, 2003
    #3
  4. 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
    Manish Jethani, Jul 25, 2003
    #4
  5. On Fri, 25 Jul 2003 14:57:57 +0200, Alexander Eisenhuth <> 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
    Bengt Richter, Jul 25, 2003
    #5
  6. Alexander Eisenhuth

    Bob Gailer Guest

    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

    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
    Bob Gailer, Jul 25, 2003
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. senthil
    Replies:
    5
    Views:
    1,377
    senthil
    Jan 24, 2004
  2. eneko
    Replies:
    2
    Views:
    2,956
    eneko
    Oct 20, 2004
  3. Drake

    binary data conversion

    Drake, Apr 11, 2004, in forum: C Programming
    Replies:
    7
    Views:
    536
    Thomas Matthews
    Apr 12, 2004
  4. Curious Trigger
    Replies:
    2
    Views:
    1,810
    Curious Trigger
    Sep 9, 2006
  5. charko
    Replies:
    0
    Views:
    599
    charko
    May 4, 2010
Loading...

Share This Page