Convert binary string to float

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • bilbo2000@comcast.net

    Convert binary string to float

    I am trying to read in a binary file which has the data stored as 32
    bit float. I have been trying to figure out how to read this in as 32
    bit but everytime I try to read it in the best I can do is a 8 bit
    integer.
    I was able to do this using a scripting language php, and found that
    the data is stored in little endian format, so I would read 4
    characters in and reverse the string and declare it a float and the
    number would come out correct. I'm not sure exactly how to go about
    this in C++ because in some cases I read a char and then print it out
    I get '' or null.
    This seems like a task that most C++ programmers would run into but I
    have not really found any place that covers this.



    int main()
    {
    const int unsigned DATA_SIZE = 401;
    const int unsigned FIND_STRING = 99;
    char data_array[DATA_SIZE];
    char anotherstring[4];
    char backstring[4];

    std::ifstream data_file("firs tdata.dat");

    for (i = 0; i < DATA_SIZE - 1; ++i) {
    assert(i >= 0);
    assert(i < sizeof(data_arr ay)/sizeof(data_arr ay[0]));
    data_file >data_array[i];
    int newdata = data_array[i];
    std::cout << "This is the integer " << newdata << '\n';
    }
    return(0);
    }

  • red floyd

    #2
    Re: Convert binary string to float

    bilbo2000@comca st.net wrote:
    I am trying to read in a binary file which has the data stored as 32
    bit float. I have been trying to figure out how to read this in as 32
    bit but everytime I try to read it in the best I can do is a 8 bit
    integer.
    I was able to do this using a scripting language php, and found that
    the data is stored in little endian format, so I would read 4
    characters in and reverse the string and declare it a float and the
    number would come out correct. I'm not sure exactly how to go about
    this in C++ because in some cases I read a char and then print it out
    I get '' or null.
    This seems like a task that most C++ programmers would run into but I
    have not really found any place that covers this.
    >
    >
    >
    int main()
    {
    const int unsigned DATA_SIZE = 401;
    const int unsigned FIND_STRING = 99;
    char data_array[DATA_SIZE];
    char anotherstring[4];
    char backstring[4];
    >
    std::ifstream data_file("firs tdata.dat");
    >
    for (i = 0; i < DATA_SIZE - 1; ++i) {
    assert(i >= 0);
    assert(i < sizeof(data_arr ay)/sizeof(data_arr ay[0]));
    data_file >data_array[i];
    try this:
    data_file.read( reinterpret_cas t<char *>(&data_array[i]),
    sizeof data_array[i]);
    int newdata = data_array[i];
    std::cout << "This is the integer " << newdata << '\n';
    }
    return(0);
    }
    >

    Comment

    • Diwa

      #3
      Re: Convert binary string to float



      On Jan 22, 5:26 pm, bilbo2...@comca st.net wrote:
      I am trying to read in a binary file which has the data stored as 32
      bit float. I have been trying to figure out how to read this in as 32
      bit but everytime I try to read it in the best I can do is a 8 bit
      integer.
      I was able to do this using a scripting language php, and found that
      the data is stored in little endian format, so I would read 4
      characters in and reverse the string and declare it a float and the
      number would come out correct. I'm not sure exactly how to go about
      this in C++ because in some cases I read a char and then print it out
      I get '' or null.
      This seems like a task that most C++ programmers would run into but I
      have not really found any place that covers this.
      >
      int main()
      {
      const int unsigned DATA_SIZE = 401;
      const int unsigned FIND_STRING = 99;
      char data_array[DATA_SIZE];
      Shouldn't this be "unsigned char" if you are
      reading binary data from stream ?
      char anotherstring[4];
      char backstring[4];
      >
      std::ifstream data_file("firs tdata.dat");
      >
      for (i = 0; i < DATA_SIZE - 1; ++i) {
      assert(i >= 0);
      assert(i < sizeof(data_arr ay)/sizeof(data_arr ay[0]));
      data_file >data_array[i];
      int newdata = data_array[i];
      std::cout << "This is the integer " << newdata << '\n';
      }
      return(0);
      >
      >
      >
      }- Hide quoted text -- Show quoted text -

      Comment

      Working...