• Advertisement
Sign in to follow this  

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

This topic is 4431 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

[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]

Share this post


Link to post
Share on other sites
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]);
}

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Thx both of you i prefer the method ZQJ uses, but how do i get the int back from that data[4] block?

Thx

Share this post


Link to post
Share on other sites

#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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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);

Share this post


Link to post
Share on other sites
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".

Share this post


Link to post
Share on other sites
main() returns int. 'void main()' is not, nor ever was legal standard C or C++.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement