Read 4 Byte floating point numbers file and save it as decimal numbers c++ | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Read 4 Byte floating point numbers file and save it as decimal numbers c++

I have a binary file that has a matrix 3601*3601 stored as 2 Byte Integer numbers, I found a code (posted below) that convert it to decimal numbers. My question: I have now a matrix 3601*3601 stored in a binary file 4 Byte floating point, how to edit my code to read this file, then output it as decimal? https://code.sololearn.com/cq3SVLfRvPBk/?ref=app

18th Jul 2018, 4:18 PM
Khaled
Khaled - avatar
1 Answer
+ 3
So, in a nutshell, you need to read floats from a binary file stored in the binary form? Well, firstly, floats are stored in the IEEE representation, which is divided like this : S XXXXXXX YYYYYYYYYYYYYYYYYYYYYYYY Where : S = Sign Bit, X = Exponent Bits (Base 2 exponent), and Y = Mantissa Bits. Now, you will have to first convert the mantissa to an interpretable form, and then you can work on the conversion part. But do note that you can just attempt to read the binary data directly to a float variable and the compiler will automatically store it in the required decimal representation. So, if you try : float f; file.read(reinterpret_cast<char*>(&f),sizeof(f)); You can retrieve the decimal form directly.
19th Aug 2018, 2:01 PM
Kinshuk Vasisht
Kinshuk Vasisht - avatar