hexadecimal ( WORD)

M

Magix

Hi,

I have following question in hex, regarding converting Dec(which is greater
than 255, to hex ( 2 bytes/ WORD). How can I convert and put the total
checksum of a string, and put it in hex char format. See below example.

Let say I have these function.

comm_send(char p) // transmit a character via serial port
{
....

}

void func1(char *str)
{
// I would like to calculate the checksum in WORD (2 byte)

for (i=0; i<strlen(str);i++)
{
checksum = checksum+char_to_dec(str)
}

if (checksum<= 255)
{
comm_send(0x00);
comm_send((char)checksum));
}
else // checksum is > 255
{
// HOW CAN I get the parameter value in the comm_send below, as
mention in my example below? Any algorithm ?
comm_send(...)
comm_send(...)

// example: if the checksum is 500 in decimal, in hex value
// it will be 1F4, I would like to
// show it as 0x01 (MSB) and 0xF4 (LSB), which will be
// comm_send(0x01);
// comm_send(0xF4);

}
}



Is there any built in function to convert decimal to hex ? If using HIBYTE
and LOBYTE Macros, how to apply them ?

Thanks.
 
C

Charles Goorsky

you could do something like:

// HOW CAN I get the parameter value in the comm_send below, as
mention in my example below? Any algorithm ?
comm_send((char)(checksum / 256))
comm_send((char)(checksum % 256))
modding it by 256 will give you the LSB,
you can always try the shift operator, which is >> or <<.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top