4 digits hex (WORD)

M

Magix

Hi,

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 can put p in 4 digits hex ?
for (i=0; i<strlen(p); i++)
{
// convert p to 4 digits hex
//checksum = checksum + p in 4 digits hex
}

How to add all of them in total to have the checksum ? checksum is 16 bits.

once I have the Checksum, let say 0x3450, I want to split them into MSB, and
LSB
send_char(0x34);
send_char(0x50);

void send_char(char p)
{
// send char
}
 
C

Case

Magix said:
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

0x0020 ( )
0x0054 (T)
0x0065 (e)
0x0073 (s)
0x0074 (t)

How can put p in 4 digits hex ?
for (i=0; i<strlen(p); i++)
{
// convert p to 4 digits hex


When you're adding, don't have to convert anything if checksum
is 16-bit. Otherwise, simply assign to a 16-bit temp. variable.
//checksum = checksum + p in 4 digits hex
}

How to add all of them in total to have the checksum ? checksum is 16 bits.


We can't decide for you how your checksum algorithm should work.
By simply adding, your 16 bit integer can become too small for
large strings. You'll have to decide what to do with the bits
that fall off. Sometimes checksum algorithms use the bit-wise
XOR operation, maybe that's something you would like to look in
to. BTW, did you Googled your way to 'checksum algorithm' already?
once I have the Checksum, let say 0x3450, I want to split them into MSB, and
LSB

You get the LSB with (checksum & 0xff) and MSB with (checksum >> 8),
at least when checksum is unsigned.

Case
 
M

Magix

My checksum algorithm is just to add up all the data (in example below,
T+e+s+t). Any overflow, it will be discarded.


Case said:
Magix said:
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

0x0020 ( )
0x0054 (T)
0x0065 (e)
0x0073 (s)
0x0074 (t)

How can put p in 4 digits hex ?
for (i=0; i<strlen(p); i++)
{
// convert p to 4 digits hex


When you're adding, don't have to convert anything if checksum
is 16-bit. Otherwise, simply assign to a 16-bit temp. variable.
//checksum = checksum + p in 4 digits hex
}

How to add all of them in total to have the checksum ? checksum is 16
bits.

We can't decide for you how your checksum algorithm should work.
By simply adding, your 16 bit integer can become too small for
large strings. You'll have to decide what to do with the bits
that fall off. Sometimes checksum algorithms use the bit-wise
XOR operation, maybe that's something you would like to look in
to. BTW, did you Googled your way to 'checksum algorithm' already?
once I have the Checksum, let say 0x3450, I want to split them into MSB, and
LSB

You get the LSB with (checksum & 0xff) and MSB with (checksum >> 8),
at least when checksum is unsigned.

Case
 

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,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top