Double to Byte

Hey guys,
Can anyone provide a simple way to convert from a double value to a byte. I need to send a double in a CAN message to a system controller. I am not using unicode libraries and my application is dialog based. Thanks!
Well, your Double value is made up of 8 bytes.
You have two choices: 1. Truncate, 2. Cast.
Truncating obviously means converting the number, and results in a big precision loss.
Casting means that if you can use pointers, you can send a pointer to this double, so you won't notice precision loss.
Probably this is the way to go, if you don't feel comfortable with pointers you should really look them up.
Casting means that if you can use pointers, you can send a pointer to this double, so you won't notice precision loss.
Probably this is the way to go, if you don't feel comfortable with pointers you should really look them up.

What's the system controller going to do with this 1-byte pointer?
Maybe he doesn't want to cast a double to a byte but send it as individual bytes.
In that case the best way is to use an union.

For example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>

int main()
{
	union{
	double myDouble;
	unsigned char myChars[sizeof(double)];
	} test;

	test.myDouble = 3.14;
	for( int k = 0; k < sizeof(double); k++ )
            std::cout << (int)test.myChars[k] << ' ';

    return 0;
}


Endianness should also be taken into account.
FWIW, using unions that way is unsupported (and undefined) by the C++ standard and therefore is not guaranteed to work - even though most compilers tend to implement them that way.
@Athar:

Read twice this part:

EssGeEich wrote:
Casting means that if you can use pointers [...]


toum wrote:
Maybe he doesn't want to cast a double to a byte but send it as individual bytes.
In that case the best way is to use an union.

I guess using pointers requires less code - just a bit more knowledge achieving the same thing.
Last edited on
Here is what I ended up doing. It works perfectly!
WORD cells;
cells = (WORD)NumberOfCells;

sendCANindexedbytes[1] = (BYTE)cells;

sendCANindexedbytes[2] = (BYTE)(cells >> 8);

Thanks for the help!
So where was the double?
I think he was talking about a double-byte word.
In the embedded world, most of the chips have 8 or 16-bit CPUs.

2 important things:
- if the devices communicating through the CAN do not have the same CPU, endianness should be taken into account
- if WORD is a signed 16-bit word, then you must take care of sign extension :
sendCANindexedbytes[2] = (BYTE) ((cells >> 8) & 0xFF);
Last edited on
I had a function that takes in a double and that is where my double comes from.

function (double NumberOfCells)
{
cells = (WORD)NumberOfCells;

sendCANindexedbytes[1] = (BYTE)cells;

sendCANindexedbytes[2] = (BYTE)(cells >> 8);

fuction to send CAN message();
}

Also the message is a 16 bit unsigned integer (obviously stored in 2 bytes). So I took all of that into account when I was casting the doubles to WORDs and BYTEs.
Topic archived. No new replies allowed.