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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Alexander Eisenhuth

    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

  • Alexander Eisenhuth

    #2
    Re: data conversion question (binary string to 'real string')

    Manish Jethani wrote:[color=blue]
    > Alexander Eisenhuth wrote:
    >
    >[color=green]
    >>maby I don't see the forest because of all that trees, but :[/color]
    >
    >
    > Maybe I don't understand your question...
    >
    >[color=green]
    >>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" ?????[/color]
    >
    >
    > ???????????????
    > What is your question?
    >
    > -Manish
    >[/color]
    Sorry ...

    This two lines converts 0.5 to a string in binary
    representation
    [color=blue][color=green][color=darkred]
    >>>from struct import *
    >>>bin_str = pack('!f', 0.5)[/color][/color][/color]

    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

    Comment

    • Alexander Eisenhuth

      #3
      Re: data conversion question (binary string to 'real string')

      Manish Jethani wrote:[color=blue]
      > Alexander Eisenhuth wrote:
      >
      >[color=green]
      >>maby I don't see the forest because of all that trees, but :[/color]
      >
      >
      > Maybe I don't understand your question...
      >
      >[color=green]
      >>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" ?????[/color]
      >
      >
      > ???????????????
      > What is your question?
      >
      > -Manish
      >[/color]
      Sorry ...

      This two lines converts 0.5 to a string in binary
      representation
      [color=blue][color=green][color=darkred]
      >>>from struct import *
      >>>bin_str = pack('!f', 0.5)[/color][/color][/color]

      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


      Comment

      • Manish Jethani

        #4
        Re: data conversion question (binary string to 'real string')

        Alexander Eisenhuth wrote:
        [color=blue]
        > This two lines converts 0.5 to a string in binary
        > representation
        >[color=green][color=darkred]
        > >>>from struct import *[/color][/color][/color]

        from binascii import hexlify
        [color=blue][color=green][color=darkred]
        > >>>bin_str = pack('!f', 0.5)[/color][/color]
        >
        > 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 ??[/color]

        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

        Comment

        • Bengt Richter

          #5
          Re: data conversion question (binary string to 'real string')

          On Fri, 25 Jul 2003 14:57:57 +0200, Alexander Eisenhuth <stacom@staco m-software.de> wrote:
          [color=blue]
          >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" ?????
          >
          >[/color]
          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 :
          [color=blue][color=green][color=darkred]
          >>> [c for c in struct.pack('!f ', 0.5)][/color][/color][/color]
          ['?', '\x00', '\x00', '\x00'][color=blue][color=green][color=darkred]
          >>> [ord(c) for c in struct.pack('!f ', 0.5)][/color][/color][/color]
          [63, 0, 0, 0][color=blue][color=green][color=darkred]
          >>> ['%02X'%ord(c) for c in struct.pack('!f ', 0.5)][/color][/color][/color]
          ['3F', '00', '00', '00']

          And finally, all you need is
          [color=blue][color=green][color=darkred]
          >>> ''.join(['%02X'%ord(c) for c in struct.pack('!f ', 0.5)])[/color][/color][/color]
          '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

          Comment

          • Bob Gailer

            #6
            Re: data conversion question (binary string to 'real string')

            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:[color=blue][color=green][color=darkred]
            >>> [13>>x & 1 for x in range(6,-1,-1)][/color][/color][/color]
            [0, 0, 0, 1, 1, 0, 1]

            Bob Gailer
            bgailer@alum.rp i.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

            Comment

            Working...