Stripping Hex and then ASCII

Discussion in 'C Programming' started by Colin Green, Jul 7, 2004.

  1. Colin Green

    Colin Green Guest

    Hi this is for Serial Com.. On the 'send side'. I will be sending, for
    example "how are you?". It will be then coupled with the Header
    Code(0x34) and the Checksum (0x0C). The whole string will be 0x34 0x48
    0x6F 0x77 0x20 0x61 0x72 0x65 0x20 0x79 0x6F 0x75 0x3F 0x0C. However
    what is actually sent is 34 486F772061726520796F753F 0C.

    My questions are to write them in C
    1) How to strip the Header and Checksum from the actual message?
    2) How to convert the 486F772061726520796F753F into ASCII again? (that
    means on the receiving end I should see "how are you?"

    I need to store them on the buffer

    Any code that woudl help will be appreciated

    yours
    Green
     
    Colin Green, Jul 7, 2004
    #1
    1. Advertising

  2. In 'comp.lang.c', (Colin Green) wrote:

    > Hi this is for Serial Com.. On the 'send side'. I will be sending, for
    > example "how are you?". It will be then coupled with the Header
    > Code(0x34) and the Checksum (0x0C). The whole string will be 0x34 0x48
    > 0x6F 0x77 0x20 0x61 0x72 0x65 0x20 0x79 0x6F 0x75 0x3F 0x0C. However
    > what is actually sent is 34 486F772061726520796F753F 0C.
    >
    > My questions are to write them in C
    > 1) How to strip the Header and Checksum from the actual message?
    > 2) How to convert the 486F772061726520796F753F into ASCII again? (that
    > means on the receiving end I should see "how are you?"
    >
    > I need to store them on the buffer
    >
    > Any code that woudl help will be appreciated


    You want sprintf() (encode) and strncat() + strtol() (decode). But we are not
    going to write your assignment for you. It's your job.

    That said, you can get some ideas here:

    http://mapage.noos.fr/emdel/clib.htm
    Module S

    --
    -ed- get my email here: http://marreduspam.com/ad672570
    The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
    C-reference: http://www.dinkumware.com/manuals/reader.aspx?lib=c99
    FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
     
    Emmanuel Delahaye, Jul 7, 2004
    #2
    1. Advertising

  3. Colin Green wrote:
    > Hi this is for Serial Com.. On the 'send side'. I will be sending, for
    > example "how are you?". It will be then coupled with the Header
    > Code(0x34) and the Checksum (0x0C). The whole string will be 0x34 0x48
    > 0x6F 0x77 0x20 0x61 0x72 0x65 0x20 0x79 0x6F 0x75 0x3F 0x0C. However
    > what is actually sent is 34 486F772061726520796F753F 0C.
    >
    > My questions are to write them in C
    > 1) How to strip the Header and Checksum from the actual message?
    > 2) How to convert the 486F772061726520796F753F into ASCII again? (that
    > means on the receiving end I should see "how are you?"
    >
    > I need to store them on the buffer
    >
    > Any code that woudl help will be appreciated
    >
    > yours
    > Green


    Try using a union:

    union
    {
    unsigned char data[13];

    struct
    {
    unsigned char header;
    char message[11];
    unsigned char checksum;
    } values;
    } packet;

    On the send end:

    packet.values.header = 0x34;
    packet.values.checksum = 0x0C;
    strncpy(packet.values.message, "how are you?", 11);

    Then send the packet something like:
    write(serialPortHandle, packet.data, 13);

    On the receive side, just read like:
    read(serialPortHandle, packet.data, 13);

    Then extract the values from the union.

    char receivedMessage[11];

    header = packet.values.header;
    header = packet.values.checksum;
    strncpy(receivedMessage, packet.values.message, 11);

    If you're going to use the receivedMessage as a string, you
    need to remember to add a terminating null, with will require
    that receivedMessage be at least 1 byte longer than the maximum
    size of the message you expect.
     
    Drew MacDonald, Jul 7, 2004
    #3
  4. In 'comp.lang.c', Drew MacDonald <> wrote:

    >> Hi this is for Serial Com.

    <...>
    > Try using a union:
    >
    > union
    > {
    > unsigned char data[13];
    >
    > struct
    > {
    > unsigned char header;
    > char message[11];
    > unsigned char checksum;
    > } values;
    > } packet;
    >
    > On the send end:
    >
    > packet.values.header = 0x34;
    > packet.values.checksum = 0x0C;
    > strncpy(packet.values.message, "how are you?", 11);
    >
    > Then send the packet something like:
    > write(serialPortHandle, packet.data, 13);
    >
    > On the receive side, just read like:
    > read(serialPortHandle, packet.data, 13);
    >
    > Then extract the values from the union.
    >
    > char receivedMessage[11];
    >
    > header = packet.values.header;
    > header = packet.values.checksum;
    > strncpy(receivedMessage, packet.values.message, 11);
    >
    > If you're going to use the receivedMessage as a string, you
    > need to remember to add a terminating null, with will require
    > that receivedMessage be at least 1 byte longer than the maximum
    > size of the message you expect.
    >


    Structures and unions are not portable and should not be used to implement
    physical interfaces.

    BTW, read() and write() and system functions. They don't belong to the
    standard C language library.

    --
    -ed- get my email here: http://marreduspam.com/ad672570
    The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
    C-reference: http://www.dinkumware.com/manuals/reader.aspx?lib=c99
    FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
     
    Emmanuel Delahaye, Jul 7, 2004
    #4
  5. Emmanuel Delahaye wrote:
    > Structures and unions are not portable and should not be used to implement
    > physical interfaces.


    I know I'm about to ask a question that will potentially label me as an
    idiot, but how are structures and unions NOT portable (as long as you
    aren't using some system specific types)?

    > BTW, read() and write() and system functions. They don't belong to the
    > standard C language library.


    I know, I was just using them as a simple example.

    Drew.
     
    Drew MacDonald, Jul 7, 2004
    #5
  6. Colin Green

    Ben Pfaff Guest

    Drew MacDonald <> writes:

    > Emmanuel Delahaye wrote:
    >> Structures and unions are not portable and should not be used to
    >> implement physical interfaces.

    >
    > I know I'm about to ask a question that will potentially label me as
    > an idiot, but how are structures and unions NOT portable (as long as
    > you aren't using some system specific types)?


    The sizes of types vary from one implementation to another, as
    does the padding inserted between and after structure and union
    members.
    --
    Just another C hacker.
     
    Ben Pfaff, Jul 7, 2004
    #6
  7. In 'comp.lang.c', Ben Pfaff <> wrote:

    >>> Structures and unions are not portable and should not be used to
    >>> implement physical interfaces.

    >>
    >> I know I'm about to ask a question that will potentially label me as
    >> an idiot, but how are structures and unions NOT portable (as long as
    >> you aren't using some system specific types)?

    >
    > The sizes of types vary from one implementation to another, as
    > does the padding inserted between and after structure and union
    > members.


    Not to mention endianness issues...

    --
    -ed- get my email here: http://marreduspam.com/ad672570
    The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
    C-reference: http://www.dinkumware.com/manuals/reader.aspx?lib=c99
    FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
     
    Emmanuel Delahaye, Jul 7, 2004
    #7
    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. Replies:
    10
    Views:
    6,409
    Neredbojias
    Aug 19, 2005
  2. Bengt Richter
    Replies:
    6
    Views:
    536
    Juha Autero
    Aug 19, 2003
  3. David Pratt

    Stripping ASCII codes when parsing

    David Pratt, Oct 14, 2005, in forum: Python
    Replies:
    2
    Views:
    1,044
    Erik Max Francis
    Oct 17, 2005
  4. David Pratt

    Re: Stripping ASCII codes when parsing

    David Pratt, Oct 17, 2005, in forum: Python
    Replies:
    1
    Views:
    339
    Tony Nelson
    Oct 18, 2005
  5. Replies:
    0
    Views:
    341
Loading...

Share This Page