S
ssubbarayan
Hi all,
I am trying a program to convert floating point values to a byte array
and printing the same to the screen.The idea behind this is we already
have an existing function which can do byte level parsing what ever
may be the type of data.The data would be coming from an external
environment.When I parse int,char at byte level,I get right
values,where as floating point just prints 0.000000 to the
screen.Given below is a sample program I tried:
#include <stdio.h>
struct info
{
unsigned char day;
unsigned char month;
unsigned short year;
unsigned char arr[6];
float data;
};
struct info info1;
struct info info2;
void parser(unsigned char* data);
int main(int argc, char *argv[])
{
info1.day=31;
info1.month=8;
info1.year=2016;
info1.arr[0]='h';
info1.arr[1]= 'a';
info1.arr[2]= 'i';
info1.data=12345.50;
printf("data is %f\n",info1.data);
printf("size of info struct is %d\n",sizeof(info));
parser((unsigned char*)(&info1));
return 1;
}
void parser(unsigned char* data)
{
int i;
info2.day=data[0];
info2.month=data[1];
info2.year=(data[3]<<8)|(data[2]);
#if 0
for(i=0;i<10;i++)
{
printf("subsequent value in array is %d
\n",data);
}
#endif
for(i=4;data!='\0';i++)
{
printf("character in arr is %c\n",data);
}
printf("data in arr is %c\n",data[4]);
printf("data in arr is %c
\n",data[5]);
printf("data in arr is %c\n",data[6]);
info2.data=(float)((data[10]<<24)|(data[9]<<16)|(data[8]<<8)|
(data[7]));
printf("day is %d\n",info2.day);
printf("month is %d\n",info2.month);
printf("year is %d\n",info2.year);
printf("data is %f\n",info2.data);
}
I get correct values for all the variables with in info2 struct except
for info2.data.
info2.data,prints 0.000000 in the screen,where as assigned value is
12345.50.
Where have I done mistake?I tried searching in net and this groups,but
nothing seems explainative enough for me to understand.
Can someone help me to correct this and give me a sample program
showing how to convert floating point values to bytes in C?Any links
are also appreciated.
Looking farward for your replies and advanced thanks for the same,
Regards,
s.subbarayan
I am trying a program to convert floating point values to a byte array
and printing the same to the screen.The idea behind this is we already
have an existing function which can do byte level parsing what ever
may be the type of data.The data would be coming from an external
environment.When I parse int,char at byte level,I get right
values,where as floating point just prints 0.000000 to the
screen.Given below is a sample program I tried:
#include <stdio.h>
struct info
{
unsigned char day;
unsigned char month;
unsigned short year;
unsigned char arr[6];
float data;
};
struct info info1;
struct info info2;
void parser(unsigned char* data);
int main(int argc, char *argv[])
{
info1.day=31;
info1.month=8;
info1.year=2016;
info1.arr[0]='h';
info1.arr[1]= 'a';
info1.arr[2]= 'i';
info1.data=12345.50;
printf("data is %f\n",info1.data);
printf("size of info struct is %d\n",sizeof(info));
parser((unsigned char*)(&info1));
return 1;
}
void parser(unsigned char* data)
{
int i;
info2.day=data[0];
info2.month=data[1];
info2.year=(data[3]<<8)|(data[2]);
#if 0
for(i=0;i<10;i++)
{
printf("subsequent value in array is %d
\n",data);
}
#endif
for(i=4;data!='\0';i++)
{
printf("character in arr is %c\n",data);
}
printf("data in arr is %c\n",data[4]);
printf("data in arr is %c
\n",data[5]);
printf("data in arr is %c\n",data[6]);
info2.data=(float)((data[10]<<24)|(data[9]<<16)|(data[8]<<8)|
(data[7]));
printf("day is %d\n",info2.day);
printf("month is %d\n",info2.month);
printf("year is %d\n",info2.year);
printf("data is %f\n",info2.data);
}
I get correct values for all the variables with in info2 struct except
for info2.data.
info2.data,prints 0.000000 in the screen,where as assigned value is
12345.50.
Where have I done mistake?I tried searching in net and this groups,but
nothing seems explainative enough for me to understand.
Can someone help me to correct this and give me a sample program
showing how to convert floating point values to bytes in C?Any links
are also appreciated.
Looking farward for your replies and advanced thanks for the same,
Regards,
s.subbarayan