Convert binary string to float

B

bilbo2000

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);
}
 
R

red floyd

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:
int newdata = data_array;
std::cout << "This is the integer " << newdata << '\n';
}
return(0);
}
 
D

Diwa

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 -
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,965
Messages
2,570,148
Members
46,710
Latest member
FredricRen

Latest Threads

Top