How to send double value as 8 uint8 values?
-
Hi all, I am currently working on Serial Interface between a control box and PC and want to send/receive double values from Pc to the box as 8 uint8 values since the box only accepts in uint8 form. For that i thought as follows : to get that double value (64 bits) in binary form and get the last 8 bits with &(and) operator and shifting bits to right till all bits are finished,namely : double var; char var2send[8]; % in array var2send[0]= var & 0x00000000000000ff % last 8 bits with & operator var>>8; %shifting bits var2send[1]= var & 0x00000000000000ff var>>8; . . % and sending this array as output . But in this code, i received an error that says i cant use double with & operators so that must be converted 64 bit long form.Besides, cast function rounds my double value to the nearest integer even if i type number like 23.34353 with precision. How can i manage to do this?? or anyone has better way or suggestion? I hope i could clearly explain my problem. I would be very pleased if you could help me! Thanks a lot! Cahit
-
Hi all, I am currently working on Serial Interface between a control box and PC and want to send/receive double values from Pc to the box as 8 uint8 values since the box only accepts in uint8 form. For that i thought as follows : to get that double value (64 bits) in binary form and get the last 8 bits with &(and) operator and shifting bits to right till all bits are finished,namely : double var; char var2send[8]; % in array var2send[0]= var & 0x00000000000000ff % last 8 bits with & operator var>>8; %shifting bits var2send[1]= var & 0x00000000000000ff var>>8; . . % and sending this array as output . But in this code, i received an error that says i cant use double with & operators so that must be converted 64 bit long form.Besides, cast function rounds my double value to the nearest integer even if i type number like 23.34353 with precision. How can i manage to do this?? or anyone has better way or suggestion? I hope i could clearly explain my problem. I would be very pleased if you could help me! Thanks a lot! Cahit
This is just a guess, but can you use a
union
for this?union
{
double dValue;
struct
{
uint8 uValu1;
uint8 uValu2;
uint8 uValu3;
uint8 uValu4;
uint8 uValu5;
uint8 uValu6;
uint8 uValu7;
uint8 uValu8;
}
};
"Money talks. When my money starts to talk, I get a bill to shut it up." - Frank
"Judge not by the eye but by the heart." - Native American Proverb
-
This is just a guess, but can you use a
union
for this?union
{
double dValue;
struct
{
uint8 uValu1;
uint8 uValu2;
uint8 uValu3;
uint8 uValu4;
uint8 uValu5;
uint8 uValu6;
uint8 uValu7;
uint8 uValu8;
}
};
"Money talks. When my money starts to talk, I get a bill to shut it up." - Frank
"Judge not by the eye but by the heart." - Native American Proverb
-
or even union { double dValue; uint8 u8value[8]; }; In general, what makes stuff like this tricky is using it across different architectures, such as x86 and m68k, for example, where byte ordering is different.
Steve S Developer for hire
:doh:
"Money talks. When my money starts to talk, I get a bill to shut it up." - Frank
"Judge not by the eye but by the heart." - Native American Proverb
-
Hi all, I am currently working on Serial Interface between a control box and PC and want to send/receive double values from Pc to the box as 8 uint8 values since the box only accepts in uint8 form. For that i thought as follows : to get that double value (64 bits) in binary form and get the last 8 bits with &(and) operator and shifting bits to right till all bits are finished,namely : double var; char var2send[8]; % in array var2send[0]= var & 0x00000000000000ff % last 8 bits with & operator var>>8; %shifting bits var2send[1]= var & 0x00000000000000ff var>>8; . . % and sending this array as output . But in this code, i received an error that says i cant use double with & operators so that must be converted 64 bit long form.Besides, cast function rounds my double value to the nearest integer even if i type number like 23.34353 with precision. How can i manage to do this?? or anyone has better way or suggestion? I hope i could clearly explain my problem. I would be very pleased if you could help me! Thanks a lot! Cahit
BYTE *pBytes = (BYTE *)&var; send(pBytes[0]); send(pBytes[1]); ... send(pBytes[7]);
-
or even union { double dValue; uint8 u8value[8]; }; In general, what makes stuff like this tricky is using it across different architectures, such as x86 and m68k, for example, where byte ordering is different.
Steve S Developer for hire
-
Steve S wrote:
union { double dValue; uint8 u8value[8]; };
I would like to suggest this:
#pragma pack(1)
union
{
double dValue;
uint8 u8value[8];
};
#pragma pack()Alcohol. The cause of, and the solution to, all of life's problems - Homer Simpson
There really is no need for that. Unsigned char's will be placed on 8-bit boundaries on all common systems, and doubles will be placed on 64-bit boundaries. The union will be sized and oriented based on the largest size requirement (the double), so packing it serves no purpose.
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac