4 digits hex (WORD)

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

  1. Magix

    Magix Guest

    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
    #1
    1. Advertising

  2. Magix

    Case Guest

    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
    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
     
    Case, Jun 14, 2004
    #2
    1. Advertising

  3. Magix

    Magix Guest

    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
    > 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
    >
     
    Magix, Jun 15, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. aefxx
    Replies:
    1
    Views:
    595
    Virgil Green
    Dec 17, 2004
  2. Replies:
    10
    Views:
    6,223
    Neredbojias
    Aug 19, 2005
  3. Bengt Richter
    Replies:
    6
    Views:
    474
    Juha Autero
    Aug 19, 2003
  4. Replies:
    14
    Views:
    1,493
    Dave Thompson
    Aug 14, 2006
  5. Hex digits to special chars

    , Aug 1, 2006, in forum: C Programming
    Replies:
    8
    Views:
    333
    Herbert Rosenau
    Aug 4, 2006
Loading...

Share This Page