Re: Help with string containing null ('\0') chars ...

From: Francis Glassborow (francis_at_robinton.demon.co.uk)
Date: 05/13/04


Date: Thu, 13 May 2004 10:47:58 +0100

In message <bm45a0lek8j5vn4fqio38icqr10blp9dj2@4ax.com>, Surely
<think@wise.com> writes
>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 */
But this does not 'ignore' nulls it replaces them with whatever was
already at that point in pkt which might be anything, including a null.
So what was your intent?

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

CkSm() needs to be written to handle nulls, which means that it also
must have a second parameter identifying the length of the array being
processed. In simple terms, if you did not write CkSm() then you cannot
use it on arrays of char with embedded nulls.

-- 
Francis Glassborow      ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects


Relevant Pages

  • Re: sending echo to all clients
    ... I did initialize it up properly, ... There is only one array and that is an array of pollfd structures named ... as an array of characters only but then I can't because sendsends bytes ... you receive C-style strings, so there's really no point to doing it. ...
    (comp.unix.programmer)
  • Re: Subquery Confusion
    ... Then I got this crazy idea that an Array can only contain a maximum ... number of characters, ... Then I decide that maybe I'm completely wrong with my query, ... it out of Excel VBA and spit it into Microsoft SQL Server Management ...
    (microsoft.public.excel.programming)
  • Re: ifstream::get() surprise
    ... Extracts characters and stores them into successive locations ... > null character into the next successive location of the array. ... since the rule is to post the shortest code suffering from the ...
    (comp.lang.cpp)
  • Re: Problem with inserting bytea into postgresql
    ... > This byte array is serialized object. ... > ByteArrayOutputStream outputStream = new ByteArrayOutputStream; ... and the characters are encoded via UTF-8. ...
    (comp.lang.java.help)
  • Re: Parsing binary values in the registry
    ... how to take a string variable that could concievably be up to 300 ... and split that into either an array or multiple variables for ... every 32 characters within the original varable. ... I am trying to build a file path from a binary value in the ...
    (microsoft.public.scripting.vbscript)