getbytes for C++ native

There is a function/method in .net for getting the byte values for numbers. However, I am using native/vanilla c++. I want to be able to convert from a user entered float value to the bytes which an external program will accept.

Exact Example:
user enters 1.0
which is 1.0000000E+000
which MUST give the result of 00 00 80 3F

Here is the .net example (GetBytesSingle( 1.0F );) http://msdn.microsoft.com/en-us/library/yhwsaf3w.aspx click the c++ version.

So how can i do this in native c++? I've done a lot of researching and can't come up with a working solution.

I'm not that skilled in c++ so a detailed answer would be beneficial.

Thanks guys!
unsigned char* buf = (unsigned char*)(&myFloat);

Though of course the exact result of this will be platform dependent.
Last edited on
Union does the job.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <algorithm>

typedef unsigned char float_byte_array[sizeof(float)];

void float_to_bytes(float f, float_byte_array result) {
	union {
		float x;
		float_byte_array res;
	} u;
	u.x = f;
	std::copy(u.res, u.res + sizeof(float), result);
}

int main() {
	float_byte_array res;
	float_to_bytes(1.0f, res);

	for ( unsigned i = 0; i < sizeof(float); ++i ) {
		std::cout << std::hex << (unsigned)res[i] << " ";
	}

}
Thanks R0mai! That was exactly what I was looking for. I appreciate the complete solution as well. I tested a few inputs and it does appear to give the correct results. Now I just have to get 0 to output as 00 and convert to uppercase ie 0 0 80 3f to 00 00 80 3F. I also need to turn the 1.0f part into a variable.

Thanks for the reply hanst99 but on my system it gave me the same garbage output as other attempts have that I found on the net. Maybe I'm not using it properly. Should I be able to "cout << buf;" and get the expected result, because that doesn't work for me. Anyways thanks for the reply.

I'm going to keep the answer open for a little longer in case you guys want to make any other responses.

Thanks!
Here is my final version. I'm sure I've broken a few rules and it its probably ugly but it works for me. Thanks again for all the help.

Here's the code for those of you who may be in the same boat as I was;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include <algorithm>

using namespace std;

typedef unsigned char float_byte_array[sizeof(float)];

void float_to_bytes(float f, float_byte_array result) {
	union {
		float x;
		float_byte_array res;
	} u;
	u.x = f;
	copy(u.res, u.res + sizeof(float), result);
}

void Print_Bytes( float_byte_array thebytes )
{
	int i = 0;
	for (i = 0; i < sizeof(float); i++ )
	{
		if( int((unsigned)thebytes[i])!=0 )
			cout << uppercase << hex << (unsigned)thebytes[i] << " ";
		else
			cout << "00" << " ";
	}
}

void main()
{

	float_byte_array res;
	float thefloat = 1.0;

	float_to_bytes(thefloat, res); // ex. use 1.0f for hardcoded value
	Print_Bytes(res);

	cout << "\n\n";
	system("pause");
	return;
}
Don't use void main, it's not valid C++. You need to use int main().

Anyway, I don't think you can do that with unions. According to the standard, only one union member can be active at once, and accessing non-active members gives you undefined behavior. I *think* there might be an exception for char arrays like the one here but I don't know.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <algorithm>

using namespace std;

typedef unsigned char float_byte_array[sizeof(float)];

void Print_Bytes( float_byte_array thebytes )
{
	int i = 0;
	for (i = 0; i < sizeof(float); i++ )
	{
		if( int((unsigned)thebytes[i])!=0 )
			cout << uppercase << hex << (unsigned)thebytes[i] << " ";
		else
			cout << "00" << " ";
	}
}

int main()
{
	float thefloat = 1.0;
	Print_Bytes(reinterpret_cast<unsigned char*>(&thefloat));

	cout << "\n\n";
	system("pause");
	return;
}


Which is essentially what Hanst proposed earlier.
I've updated my code using some of everyone's suggestions. It should be noted that using my previous code doesn't output 1-9 properly as I had only fixed 0 to output as 00. The new code has been corrected. I added these functions to my program and found out that all of my integers were outputting as hex. So before leaving the function I revert back to decimal now. Included is another function for converting int to bytes as well. Thanks for the help everyone!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <algorithm>

using namespace std;

typedef unsigned char float_byte_array[sizeof(float)];
typedef unsigned char int_byte_array[sizeof(int)];

void float_to_bytes( float_byte_array thebytes )
{
	int i = 0;
	for (i = 0; i < sizeof(float); i++ )
	{
		if( int((unsigned)thebytes[i])<=9 )
			cout << "0";
		cout << uppercase << hex << (unsigned)thebytes[i] << " ";
	}
       cout << dec;
}

void int_to_bytes( int_byte_array thebytes )
{
	int i = 0;
	for (i = 0; i < sizeof(int); i++ )
	{
		if( int((unsigned)thebytes[i])<=9 )
			cout << "0";
		cout << uppercase << hex << (unsigned)thebytes[i] << " ";
	}
       cout << dec;
}

int main()
{
	float thefloat = 1.0;
	float_to_bytes(reinterpret_cast<unsigned char*>(&thefloat));

	cout << "\n\n";

	int theint = 300;
	int_to_bytes(reinterpret_cast<unsigned char*>(&theint));

	cout << "\n\n";
	system("pause");
	return 0;
}
Topic archived. No new replies allowed.