convert binary to float

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

    convert binary to float

    I have tried and tried...

    I'd like to read in a binary file, convert it's 4 byte values into
    floats, and then save as a .txt file.

    This works from the command line (import struct);

    In [1]: f = open("test2.pc0 ", "rb")
    In [2]: tagData = f.read(4)
    In [3]: tagData
    Out[3]: '\x00\x00\xc0@'

    I can then do the following in order to convert it to a float:

    In [4]: struct.unpack(" f", "\x00\x00\xc0@" )
    Out[4]: (6.0,)

    But when I run the same code from my .py file:

    f = open("test2.pc0 ", "rb")
    tagData = f.read(4)
    print tagData

    I get this (ASCII??):
    „@

    I only know how to work with '\x00\x00\xc0@' .

    I don't understand why the output isn't the same. I need a solution
    that will allow me to convert my binary file into floats. Am I close?
    Can anyone point me in the right direction?

    Thanks,
    Mason




  • George Sakkis

    #2
    Re: convert binary to float

    On Jun 1, 3:55 pm, Mason <john.gerald.ma ...@gmail.comwr ote:
    I have tried and tried...
    >
    I'd like to read in a binary file, convert it's 4 byte values into
    floats, and then save as a .txt file.
    >
    This works from the command line (import struct);
    >
        In [1]: f = open("test2.pc0 ", "rb")
        In [2]: tagData = f.read(4)
        In [3]: tagData
        Out[3]: '\x00\x00\xc0@'
    >
    I can then do the following in order to convert it to a float:
    >
        In [4]: struct.unpack(" f", "\x00\x00\xc0@" )
        Out[4]: (6.0,)
    >
    But when I run the same code from my .py file:
    >
        f = open("test2.pc0 ", "rb")
        tagData = f.read(4)
        print tagData
    >
    I get this (ASCII??):
    „@
    Remembering to put that struct.unpack() call in your module might
    help ;-)

    George

    Comment

    • Mason

      #3
      Re: convert binary to float

      On Jun 1, 5:12 pm, George Sakkis <george.sak...@ gmail.comwrote:
      On Jun 1, 3:55 pm, Mason <john.gerald.ma ...@gmail.comwr ote:
      >
      >
      >
      I have tried and tried...
      >
      I'd like to read in a binary file, convert it's 4 byte values into
      floats, and then save as a .txt file.
      >
      This works from the command line (import struct);
      >
      In [1]: f = open("test2.pc0 ", "rb")
      In [2]: tagData = f.read(4)
      In [3]: tagData
      Out[3]: '\x00\x00\xc0@'
      >
      I can then do the following in order to convert it to a float:
      >
      In [4]: struct.unpack(" f", "\x00\x00\xc0@" )
      Out[4]: (6.0,)
      >
      But when I run the same code from my .py file:
      >
      f = open("test2.pc0 ", "rb")
      tagData = f.read(4)
      print tagData
      >
      I get this (ASCII??):
      „@
      >
      Remembering to put that struct.unpack() call in your module might
      help ;-)
      >
      George
      Wow ... I did have it in there, but I forgot include it in my post.
      Anyway, this works just fine:

      f = open("test2.pc0 ", "rb")
      tagData = f.read(4)
      print struct.unpack(" f", tagData)

      Thanks for waking me up George!

      Comment

      • Mason

        #4
        Re: convert binary to float

        On Jun 1, 6:41 pm, Dennis Lee Bieber <wlfr...@ix.net com.comwrote:
        On Sun, 1 Jun 2008 12:55:45 -0700 (PDT), Mason
        <john.gerald.ma ...@gmail.comde claimed the following in
        comp.lang.pytho n:
        >
        I have tried and tried...
        >
        I'd like to read in a binary file, convert it's 4 byte values into
        floats, and then save as a .txt file.
        >
        This works from the command line (import struct);
        >
        In [1]: f = open("test2.pc0 ", "rb")
        In [2]: tagData = f.read(4)
        In [3]: tagData
        >
        Interpreter display of raw object name uses repr()
        >
        Out[3]: '\x00\x00\xc0@'
        >
        I can then do the following in order to convert it to a float:
        >
        In [4]: struct.unpack(" f", "\x00\x00\xc0@" )
        Out[4]: (6.0,)
        >
        But when I run the same code from my .py file:
        >
        f = open("test2.pc0 ", "rb")
        tagData = f.read(4)
        print tagData
        >
        Display from a print statement uses str()
        >
        I get this (ASCII??):
        „@
        >
        Probably not ASCII -- ASCII doesn't have that spanish (?) bottom row
        quote... And a pair of null bytes don't take up screen space.
        >
        I only know how to work with '\x00\x00\xc0@' .
        >
        I don't understand why the output isn't the same. I need a solution
        that will allow me to convert my binary file into floats. Am I close?
        Can anyone point me in the right direction?
        >
        Why do you have to /see/ the byte representation in the first
        place... just feed the four bytes to the struct module directly.
        >
        import struct
        fin = open("test2.pc0 ", "rb")
        tagFloat = struct.unpack(" f", fin.read(4))[0]
        print tagFloat
        --
        Wulfraed Dennis Lee Bieber KD6MOG
        wlfr...@ix.netc om.com wulfr...@bestia ria.com

        (Bestiaria Support Staff: web-a...@bestiaria. com)
        HTTP://www.bestiaria.com/
        Thanks Dennis, I'm OK now. I just sort of dropped the ball for a
        bit :).

        Mason

        Comment

        • Mark Tolonen

          #5
          Re: convert binary to float

          >"George Sakkis" <george.sakkis@ gmail.comwrote in message
          >news:829b1e8 f-baac-4ff4-909b->b39df97a436c@d 45g2000hsc.goog legroups.com...
          >On Jun 1, 3:55 pm, Mason <john.gerald.ma ...@gmail.comwr ote:
          >I have tried and tried...
          >>
          >I'd like to read in a binary file, convert it's 4 byte values into
          >floats, and then save as a .txt file.
          >>
          >This works from the command line (import struct);
          >>
          >In [1]: f = open("test2.pc0 ", "rb")
          >In [2]: tagData = f.read(4)
          >In [3]: tagData
          >Out[3]: '\x00\x00\xc0@'
          >>
          >I can then do the following in order to convert it to a float:
          >>
          >In [4]: struct.unpack(" f", "\x00\x00\xc0@" )
          >Out[4]: (6.0,)
          >>
          >But when I run the same code from my .py file:
          >>
          >f = open("test2.pc0 ", "rb")
          >tagData = f.read(4)
          >print tagData
          >>
          >I get this (ASCII??):
          >„@
          >
          Remembering to put that struct.unpack() call in your module might
          help ;-)
          >
          George
          tagData still contains your data, but it is being displayed two different
          ways. Consult the documentation about str() and repr().

          -Mark

          Comment

          Working...