variable to binary data, how is this done?[SOLVED]

Started by
7 comments, last by RDragon1 18 years, 3 months ago
[SOLVED] SOLUTION IN LAST POST Ok some code:

int my_test_int = 1337;
int * p_my_test_int = &my_test_int;
char * binarydata  =  ??
void * binarydata  =  ??
How to i put this integer into raw binary data, i want to create a 4byte representation of the int ( to write to a file ) Any suggestions? Thx Aidamina [Edited by - Aidamina on January 5, 2006 10:34:09 AM]
-->My Site<--
Advertisement
#include <stdio.h>union U{	int i;	unsigned __int8 b[4];};void main(){	int i=1337;	// First way!	U u;	u.i=i;	printf("%d %d %d %d\n",u.b[0],u.b[1],u.b[2],u.b[3]);	// Second way!	unsigned __int8 *p;	p=(unsigned __int8 *)&i;	printf("%d %d %d %d\n",p[0],p[1],p[2],p[3]);}
That depends on how portable you want to be :) If you want your binary file to be readable on all architectures, you're going to need something like:

int value = whatever;char data[4];data[0] = (char)value;data[1] = (char)(value >> 8);data[2] = (char)(value >> 16);data[3] = (char)(value >> 24);std::fstream stream("myfile.bin");stream.write(data, 4);


If you don't care, just do:

int value = whatever;std::fstream stream("myfile.bin");stream.write(reinterpret_cast<char*>(&value), sizeof(int));


Or something similar.
Thx both of you i prefer the method ZQJ uses, but how do i get the int back from that data[4] block?

Thx
-->My Site<--
#include <stdio.h>union U{	int i;	unsigned __int8 b[4];};void main(){	// 1	{	U u;	u.b[0]=57;	u.b[1]=5;	u.b[2]=0;	u.b[3]=0;	printf("%d\n",u.i);	}	// 2	{	int i;	i=0;	i|=0;i<<=8;	i|=0;i<<=8;	i|=5;i<<=8;	i|=57;	printf("%d\n",i);	}	//3	{	int i;	unsigned __int8 *p;//You can use char* too.	p=(unsigned __int8*)&i;	p[0]=57;p[1]=5;p[2]=0;p[3]=0;	printf("%d\n",i);	}}

Output:
1337
1337
1337
Or in my version

value = data[0] + (data[1] << 8) + (data[2] << 16) + (data[3] << 24);

IIRC you don't need to cast data[0..3] to int because char is automatically widened to int for arithmetic operations. I'm not entirely sure about that though.
Thx both of you, i rated++ both of you:

Just for future reference this is what i used:

int value = 1337;char data[4];data[0] = (char)value;data[1] = (char)(value >> 8);data[2] = (char)(value >> 16);data[3] = (char)(value >> 24);printf("binary content of data: |%x|%x|%x|%x|",data[0],data[1],data[2],data[3]);int i;i=0;i|=data[3];i<<=8;i|=data[2];i<<=8;i|=data[1];i<<=8;i|=data[0];printf("value of i = %d",i);
-->My Site<--
There is something you should know:
1.
(I think)The Union method should be much faster than the others because it does not involve any mathematical operations.
2.
You can write the integer data directly to a file. As long as you use the same types (specially if you want to write a cross platform application) to write and read the data, you should not have any problems.
I prefer to use __int32 instead of int because I do not know how different compilers on different PCs(16bit,32bit,64bit) are going to interpret "int".
main() returns int. 'void main()' is not, nor ever was legal standard C or C++.

This topic is closed to new replies.

Advertisement