Convert binary string to float

Discussion in 'C++' started by bilbo2000@comcast.net, Jan 22, 2007.

  1. Guest

    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("firstdata.dat");

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

  2. red floyd Guest

    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("firstdata.dat");
    >
    > for (i = 0; i < DATA_SIZE - 1; ++i) {
    > assert(i >= 0);
    > assert(i < sizeof(data_array)/sizeof(data_array[0]));
    > data_file >> data_array;


    try this:
    data_file.read(reinterpret_cast<char *>(&data_array),
    sizeof data_array);

    > int newdata = data_array;
    > std::cout << "This is the integer " << newdata << '\n';
    > }
    > return(0);
    > }
    >
    red floyd, Jan 22, 2007
    #2
    1. Advertising

  3. Diwa Guest

    On Jan 22, 5:26 pm, 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("firstdata.dat");
    >
    > for (i = 0; i < DATA_SIZE - 1; ++i) {
    > assert(i >= 0);
    > assert(i < sizeof(data_array)/sizeof(data_array[0]));
    > data_file >> data_array;
    > int newdata = data_array;
    > std::cout << "This is the integer " << newdata << '\n';
    > }
    > return(0);
    >
    >
    >
    > }- Hide quoted text -- Show quoted text -
    Diwa, Jan 26, 2007
    #3
    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. Andy
    Replies:
    7
    Views:
    6,235
    Roedy Green
    May 10, 2004
  2. bd
    Replies:
    0
    Views:
    602
  3. music4
    Replies:
    4
    Views:
    1,290
    Lew Pitcher
    Sep 20, 2003
  4. Mason

    convert binary to float

    Mason, Jun 1, 2008, in forum: Python
    Replies:
    4
    Views:
    667
  5. Carsten Fuchs
    Replies:
    45
    Views:
    1,499
    James Kanze
    Oct 8, 2009
Loading...

Share This Page