473,385 Members | 1,655 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

Jan 22 '07 #1
2 5925
bi*******@comcast.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("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[i];
try this:
data_file.read(reinterpret_cast<char *>(&data_array[i]),
sizeof data_array[i]);
int newdata = data_array[i];
std::cout << "This is the integer " << newdata << '\n';
}
return(0);
}
Jan 22 '07 #2


On Jan 22, 5:26 pm, bilbo2...@comcast.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("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[i];
int newdata = data_array[i];
std::cout << "This is the integer " << newdata << '\n';
}
return(0);

}- Hide quoted text -- Show quoted text -
Jan 26 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: jaijai_kumar | last post by:
Select Cast('100.1234' as float) give me the result 100.1234 Now when I convert it back to char I want exactly 100.1234 Select Convert(char(100),Cast('100.1234' as float)) Gives me 100.123 (Here...
4
by: aevans1108 | last post by:
expanding this message to microsoft.public.dotnet.xml Greetings Please direct me to the right group if this is an inappropriate place to post this question. Thanks. I want to format a...
4
by: music4 | last post by:
Greetings, I psinfo_t struct, pr_pctcpu is ushort_t type with comment: "pr_pctcpu is 16 bit binary fractions in the range 0.0 to 1.0 with the binary point to the right of the high-orfer bit...
27
by: fdu.xiaojf | last post by:
Hi, String formatting can be used to converting an integer to its octal or hexadecimal form: '307' 'c7' But, can string formatting be used to convert an integer to its binary form ?
28
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I convert a Number into a String with exactly 2 decimal places?...
4
by: Mason | last post by:
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 : f =...
12
by: Peter | last post by:
Trying to convert string to byte array. the following code returns byte array of {107, 62, 194, 139, 64} how can I convert this string to a byte array of {107, 62, 139, 65} ...
9
by: Marco Nef | last post by:
Hi there I'm looking for a template class that converts the template argument to a string, so something like the following should work: Convert<float>::Get() == "float"; Convert<3>::Get() ==...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.