Possible Duplicate:
Float to binary in C++可能的副本:在c++中浮动到二进制。
I want to print out the binary representation of a float number in C++. Not very practical, just out of curiosity.
我想打印出一个浮点数在c++中的二进制表示。不是很实用,只是出于好奇。
The following program doesn't compile though. The reinterpret_cast fails. What kind of cast can I use so that I can do the " &(1 << i) " part?
但是下面的程序没有编译。reinterpret_cast失败。我可以用什么类型的cast来做“&(1 < I)”“部分?
#include <iostream>
using namespace std;
void toBinary(float num) {
int numi = reinterpret_cast<int>(num);
cout << num << " " << numi << endl;
for (int i = 0; i < 8 * sizeof(num); i++){
if (numi & (1<<i)) {
cout << 1;
} else {
cout << 0;
}
}
cout << endl << endl;
}
int main() {
float a;
cout << sizeof(int) << " " << sizeof(float) << endl;
a = 13.5;
toBinary(a);
toBinary(13.9);
toBinary(2 * a);
toBinary(-a);
}
3 个解决方案
#1
16
There's a much easier way. Take a pointer to the float, and reinterpret_cast it to a pointer to char. Now loop through sizeof(float) and convert each char to 8 binary digits. This method works for doubles too.
有一个更简单的方法。取一个指向浮点数的指针,并将其重新解释为一个指向char的指针。现在循环通过sizeof(浮点数)并将每个字符转换为8个二进制数字。这种方法也适用于双打。
#2
2
Use a union. I did this code to do exactly what you want:
使用一个联盟。我做了这个代码来做你想做的:
// file floattobinary.cc
#include <string>
#include <inttypes.h> // for uint32_t
using namespace std;
void floatToBinary(float f, string& str)
{
union { float f; uint32_t i; } u;
u.f = f;
str.clear();
for (int i = 0; i < 32; i++)
{
if (u.i % 2) str.push_back('1');
else str.push_back('0');
u.i >>= 1;
}
// Reverse the string since now it's backwards
string temp(str.rbegin(), str.rend());
str = temp;
}
Below is a test program to run this function:
下面是运行这个函数的测试程序:
// file test.cc
#include <iostream>
#include <string>
#include <cstdlib> // for atof(3)
using namespace std;
void floatToBinary(float, string&);
int main(int argc, const char* argv[])
{
string str;
float f;
if (argc > 1)
{
f = static_cast<float>(atof(argv[1]));
floatToBinary(f, str);
}
cout << str << endl;
return 0;
}
Compile and run (I'm using GNU g++ on Linux):
编译和运行(我在Linux上使用GNU g++):
me@mypc:~/college/c++/utils$ g++ -c floattobinary.cc
me@mypc:~/college/c++/utils$ g++ -c test.cc
me@mypc:~/college/c++/utils$ g++ -o test *.o
me@mypc:~/college/c++/utils$ ls
floattobinary.cc floattobinary.o test* test.cc test.o
me@mypc:~/college/c++/utils$ ./test 37.73
01000010000101101110101110000101
me@mypc:~/college/c++/utils$ ./test 2.0
01000000000000000000000000000000
me@mypc:~/college/c++/utils$ ./test 0.0
00000000000000000000000000000000
me@mypc:~/college/c++/utils$ ./test 237.74
01000011011011011011110101110001
me@mypc:~/college/c++/utils$ ./test 2.74e12
01010100000111110111110100101111
me@mypc:~/college/c++/utils$ ./test 2.74e13
01010101110001110101110001111010
me@mypc:~/college/c++/utils$ ./test -88.37
11000010101100001011110101110001
#3
0
http://www.java2s.com/Tutorial/Cpp/0100__Development/Usingreinterpretcastcastfloattoint.htm
http://www.java2s.com/Tutorial/Cpp/0100__Development/Usingreinterpretcastcastfloattoint.htm
#1
16
There's a much easier way. Take a pointer to the float, and reinterpret_cast it to a pointer to char. Now loop through sizeof(float) and convert each char to 8 binary digits. This method works for doubles too.
有一个更简单的方法。取一个指向浮点数的指针,并将其重新解释为一个指向char的指针。现在循环通过sizeof(浮点数)并将每个字符转换为8个二进制数字。这种方法也适用于双打。
#2
2
Use a union. I did this code to do exactly what you want:
使用一个联盟。我做了这个代码来做你想做的:
// file floattobinary.cc
#include <string>
#include <inttypes.h> // for uint32_t
using namespace std;
void floatToBinary(float f, string& str)
{
union { float f; uint32_t i; } u;
u.f = f;
str.clear();
for (int i = 0; i < 32; i++)
{
if (u.i % 2) str.push_back('1');
else str.push_back('0');
u.i >>= 1;
}
// Reverse the string since now it's backwards
string temp(str.rbegin(), str.rend());
str = temp;
}
Below is a test program to run this function:
下面是运行这个函数的测试程序:
// file test.cc
#include <iostream>
#include <string>
#include <cstdlib> // for atof(3)
using namespace std;
void floatToBinary(float, string&);
int main(int argc, const char* argv[])
{
string str;
float f;
if (argc > 1)
{
f = static_cast<float>(atof(argv[1]));
floatToBinary(f, str);
}
cout << str << endl;
return 0;
}
Compile and run (I'm using GNU g++ on Linux):
编译和运行(我在Linux上使用GNU g++):
me@mypc:~/college/c++/utils$ g++ -c floattobinary.cc
me@mypc:~/college/c++/utils$ g++ -c test.cc
me@mypc:~/college/c++/utils$ g++ -o test *.o
me@mypc:~/college/c++/utils$ ls
floattobinary.cc floattobinary.o test* test.cc test.o
me@mypc:~/college/c++/utils$ ./test 37.73
01000010000101101110101110000101
me@mypc:~/college/c++/utils$ ./test 2.0
01000000000000000000000000000000
me@mypc:~/college/c++/utils$ ./test 0.0
00000000000000000000000000000000
me@mypc:~/college/c++/utils$ ./test 237.74
01000011011011011011110101110001
me@mypc:~/college/c++/utils$ ./test 2.74e12
01010100000111110111110100101111
me@mypc:~/college/c++/utils$ ./test 2.74e13
01010101110001110101110001111010
me@mypc:~/college/c++/utils$ ./test -88.37
11000010101100001011110101110001
#3
0
http://www.java2s.com/Tutorial/Cpp/0100__Development/Usingreinterpretcastcastfloattoint.htm
http://www.java2s.com/Tutorial/Cpp/0100__Development/Usingreinterpretcastcastfloattoint.htm