# 4 digits hex (WORD)

Discussion in 'C Programming' started by Magix, Jun 14, 2004.

1. ### MagixGuest

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
}

Magix, Jun 14, 2004

2. ### CaseGuest

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

>
> 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

Case, Jun 14, 2004

3. ### MagixGuest

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" <> wrote in message
news:40cdce06\$0\$48933\$4all.nl...
> Magix wrote:
> > 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
>
> >
> > 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
>

Magix, Jun 15, 2004