Re: Help with string containing null ('\0') chars ...
From: Surely (think_at_wise.com)
Date: 05/13/04
- Next message: Curley Q.: "Re: [C][OT] erroneous fprintf output"
- Previous message: Surely: "Re: Help with string containing null ('\0') chars ..."
- In reply to: Neil Kurzman: "Re: Help with string containing null ('\0') chars ..."
- Next in thread: Francis Glassborow: "Re: Help with string containing null ('\0') chars ..."
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Thu, 13 May 2004 21:28:03 GMT
On Thu, 13 May 2004 04:47:43 GMT, Neil Kurzman <nsk@mail.asb.com>
wrote:
>
>
>Surely wrote:
>
>> I am trying to checksum an array of characters which is the string:
>>
>> static char prog_msg[] = { "\0\0\0$1001FFF@80001112131AF7D\r" };
>>
>> I have tried various kinds of loops to assign each char in the above
>> string (prog_msg) to another indexed variable (pkt) needed for the
>> CkSm function. Example:
>>
>> i = 0;
>> len = sizeof (prog_msg);
>>
>> while (i < len)
>>
>> {
>>
>> if (prog_msg[i] != '\0') /* if null move past it */
>>
>> { pkt[i] = prog_msg[i]; } /* otherwise, add it to pkt */
>>
>> ++i; /* then increment counter */
>>
>>
>> }
>>
>> CkSm(pkt);
>>
>> The process fails because each of the nulls make the routine think
>> that pkt is terminated. Therefore it doesn't progress through to
>> the actual end of the string. The result is that pkt has no
>> characters to pass off to CkSm. The prog_msg ACTUALLY is part of a
>> received message protocol, a remote command from the outside world,
>> so I can't just ignore the null's.
>>
>> How can all of the characters be assigned (including the null's) into
>> pkt?
>>
>> I'm using ANSI C, with a Microtech Research Inc MCCM77 compiler.
>>
>> Thank you.
>
>I think the problem is CkSm(pkt);
>if you wrote CkSm(pkt,sizeof (prog_msg));
>You would not have to bother with the nulls.
>
I think you have a better idea here. The prog_msg is actually a
received string from a serial com port ; starting with 3 null chars,
the body of the message, and followed by a checksum and '\r'.
I will need to validate the checksum on each incoming message, and
then format a response message with a checksum before being returned
to the remote host.
Hence the need for a generic CkSm() function. I guess the part I was
missing was the sizeof(prog_msg) piece. I had, by the way, already
defined the pkt buffer to be an array of 256 chars, as that is the max
length of any received message.
Maybe I don't need to assign a copy of he prog_msg to the pkt variable
at all. I was thinking it was necessary in order to preserve the
original message while the checksum computation worked on the copy in
the pkt variable. There's probably a better way to do that, I'll have
to try think that through.
Thanks for your help!
- Next message: Curley Q.: "Re: [C][OT] erroneous fprintf output"
- Previous message: Surely: "Re: Help with string containing null ('\0') chars ..."
- In reply to: Neil Kurzman: "Re: Help with string containing null ('\0') chars ..."
- Next in thread: Francis Glassborow: "Re: Help with string containing null ('\0') chars ..."
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]