Click to See Complete Forum and Search --> : hexadecimal in WORD (2 bytes) format
Dimension
June 10th, 2004, 10:45 AM
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[i])
}
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);
}
}
BorisKK
June 10th, 2004, 12:02 PM
Use HIBYTE and LOBYTE macros for MSB and LSB, respectively.
Dimension
June 10th, 2004, 12:20 PM
How to apply them (LOBYTE and HIBYTE) ? ANy example?
Is there any built-in functiont o convert decimal to hex ? and how can I put the hex as an char?
Marc G
June 10th, 2004, 12:48 PM
char str[16];
sprintf(str, "%X", my_integer_number);
// str is now the hex string representation of my_integer_number
Dimension
June 11th, 2004, 06:01 AM
can I just
comm_send((char)str) ?
As my comm_send function is only can send byte by byte,
What if the str (hex format) is more than a byte, namely WORD?
How can I split the WORD into 2 separate byte ?
Any algorithm to do that by shifting the bits, etc (<<) ?
What I have in mind in like this, but not sure how to implement it
int HIBYTE;
int LOBYTE;
HIBYTE = Use shift operator (how?)
LOBYTE = Use shift operator (how?)
Then,
comm_send((char)HIBYTE);
comm_send((char)LOBYTE);
Any tips?
Marc G
June 11th, 2004, 06:33 AM
Do you need to send the hex value as a string or just as an integer?
To send 16 bit integer as 2 times 8 bit:
// Send 16 bit WORD
int myint = 48523;
comm_send(HIBYTE(myint));
comm_send(LOBYTE(myint));
or the other way around, depending on which part you want to receive first.
Dimension
June 11th, 2004, 11:32 AM
I need to send char. Like what I said in my example above.
int myint=500;
then I can send:
comm_send(0x01);
comm_send(0xF4);
I will try out your method. Thanks.
Dimension
June 11th, 2004, 11:47 AM
I have other related question.
Let say I have these string
Example 1: char *str="1234";
Example 2: char *str2="5678999";
Example 3: char *str3=NULL;
I have 10 bytes (5 WORD) to allocate. Two characters will become an hex value. Example: str[0] and str[1] will combine become hex value. If NULL, it will represent as FF. See below example.
In Example 1: I will need to send this
comm_send(0x12)
comm_send(0x34)
comm_send(0xFF)
comm_send(0xFF)
comm_send(0xFF)
comm_send(0xFF)
comm_send(0xFF)
comm_send(0xFF)
comm_send(0xFF)
comm_send(0xFF)
In Example 2: I will need to send this
comm_send(0x56)
comm_send(0x78)
comm_send(0x9F)
comm_send(0xFF)
comm_send(0xFF)
comm_send(0xFF)
comm_send(0xFF)
comm_send(0xFF)
comm_send(0xFF)
comm_send(0xFF)
In Example 3: I will need to send this
comm_send(0xFF)
comm_send(0xFF)
comm_send(0xFF)
comm_send(0xFF)
comm_send(0xFF)
comm_send(0xFF)
comm_send(0xFF)
comm_send(0xFF)
comm_send(0xFF)
comm_send(0xFF)
any algorithm to do this?
euank
June 11th, 2004, 01:11 PM
it's called memset.
Set the buffer to all f's before you start. Of course this would require that you generate the packet in a single buffer before you send anything.
That's too easy a question!
And for converting the CRC to hex string then it's:
sprintf(&buffer[position], "%04X", checksum)
the 04X means that the chars are padded with '0' chars if the value is only less than values.
IE 0x1 is written as "0001"
500 decimal becoms a string of "01f4"
Of course this all depends on whether your protocol is ascii or binary.
500 in hex is 4 bytes for ascii. 2 bytes for binary representation. Judging by your checksum example you're in binary mode.
When you want to chop a large number up into smaller numbers, you have to remember that a big number is just more bytes in memory. A char is 1, short is two, integer is 4... So you can do various things. You can use a char * to the int address which will get a bytes
worth of data, then get the next byte by incrementing the pointer:
char* ptr = (char*) checksum;
send_char(*ptr);
ptr++
send_char(*ptr); ...
or you can do bit shifting which is what the high byte low byte does:
short temp = checksum & 0x00FF; // gets rid of everthing except the first byte (or cheat char x = (char) checksum)
send_char((char)temp);
temp = (checksum & 0xFF00) >> 8; // get rid of first byte move the top byte along to where the first byte was.
send_char((char)temp);
Dimension
June 14th, 2004, 06:58 AM
Thanks.
I would like to add the char of string in hex (WORD format) to have the checksum (in WORD).
E.g char *p =" Test"
so in hex (WORD), it will be
0x0054 (T)
0x0065 (e)
0x0073 (s)
0x0074 (t)
How to add all of them in total to have the checksum ? checksum is 16 bits.
Then I would use:
short temp = checksum & 0x00FF;
send_char((char)temp);
temp = (checksum & 0xFF00) >> 8;
send_char((char)temp);
euank
June 15th, 2004, 01:47 PM
//you have your working buffer:
WORD buffer[MAX_PACKET_LENGTH];
// terminate if your playing with strings
buffer[MAX_PACKET_LENGTH-1] = 0;
// create packet to send
buffer[0] = 0x0054; // (T)
buffer[1] = 0x0065; // (e)
buffer[2] = 0x0073; // (s)
buffer[3] = 0x0074; // (t)
buffer[0] = 0; // terminate
// create checksum
WORD* position = &buffer[0]; //&...[0] for clarity
WORD checksum = 0;
while (*position) // dereference pointer to get actual word value, if it's not zero it's good.
{
if ((buffer - position) < MAX_PACKET_LENGTH)
{
checksum += *position;
// you could do "send_WORD(*position);" here if you really wanted to
position++;
}
else
{
// packet to large
return 0;
}
}
// append checksum to packet
buffer[position] = checksum;
// again you could do "send_WORD(*position);" here if you really wanted to
position++;
buffer[position] = 0; //terminate
// I prefer to send packet the entire packet in one go
write("com1", &buffer, wcslen(buffer)); // wcslen only if unicode type text really is being sent!
Personally I prefer not to use pointers to memory when using buffers (strings). I think it's cleaner and safer to keep an interger count of the size of your packet you're creating, then use that to send the completed buffer in one go.
Note; the above code is just off the top of my head. it probably has some errors in it. expecially with the pointers stuff. ;)
euank
June 15th, 2004, 01:57 PM
or for an ascii buffer:
//you have your working buffer:
char buffer[MAX_PACKET_LENGTH];
// terminate if your playing with strings
buffer[MAX_PACKET_LENGTH-1] = 0;
// create packet to send
buffer[0] = 0x54; // (T)
buffer[1] = 0x65; // (e)
buffer[2] = 0x73; // (s)
buffer[3] = 0x74; // (t)
buffer[0] = 0; // terminate
// create checksum
char* position = &buffer[]; //&...[0] for clarity
WORD checksum = 0;
while (*position) // dereference pointer to get actual word value, if it's not zero it's good.
{
if ((buffer - position) < MAX_PACKET_LENGTH)
{
checksum += *position;
// you could do "send_char(*position);" here if you really wanted to
position++;
}
else
{
// packet to large
return 0;
}
}
// append checksum to packet
WORD temp = checksum & 0x00FF;
*position = (char)temp; // or send_char((char)temp);
position++;
temp = (checksum & 0xFF00) >> 8;
*position = (char)temp; // or send_char((char)temp);
position++;
*position = 0; //termiate string
// I prefer to send packet the entire packet in one go
write("com1", (char*)&buffer, strlen(buffer)); // strlen only if buffer is zero terminated char array type text really is being sent!
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.