This has been discussed at great length on this forum - searching the
archives is a good policy. I can, however, re-post what I posted before
regarding packing of strings.
If you are simply DmWrite'ing a string, write one byte more than StrLen
returns and you will automatically write the NULL to the end. If you have a
more complex situation, however, you may need some custom code:
My original post...
Subject: How to append NULL-char to string?
From: Matthew Bevan <[EMAIL PROTECTED]>
Date: Sun, 24 Nov 2002 05:16:44 +0000
Anyway... when I need to do things like this (and you know the length of the
string, that's kinda why the nulls are there btw) I would use the following
(which is faster than a strcpy any day):
Char *p, *q, *srcstr, *deststr;
UInt16 I, strlength;
deststr = (Char *)MemPtrNew(sizeof(Char)*(strlength+1));
for ( i = 0, p = srcstr, q = deststr; i < strlength; i++ ) {
*q++ = *p++; // copy the string, letter for letter
} *q = 0; // add the null at the end
I haven't tested this, but it should work. Note that almost none of the
methods described here deal well with double-byte encoded strings. I've
removed the code using MemPtrResize... too many issues with that function.
If you have a buffer you are writing to with a pointer...
Char *p, buffer[1024];
p = buffer;
while ( WeHaveData ) {
*p++ = inputByte; // write some stuff into the buffer
} *p = 0; // add the null to the end of the string
Only do this if you are very sure you'll never have a buffer overflow,
however. Yet -another- way of doing this would be to:
Char *src, *dest;
UInt16 strlength; // 1 based... 1 char, strlength = 1
dest = (Char *)MemPtrNew(sizeof(Char)*(strlength+1));
MemMove(dest, src, strlength);
*(dest+strlength) = 0; // add the null to the end
// the above can also be written dest[strlength] = 0;
As you can see, there are a bunch of ways to do this. In my original post
from 2002 the main question was how to get an array of strings saved into a
record - many strings seperated by nulls. Though not exactly your question,
I'll re-post the packing code again so it might answer other's questions
when they look at the archives. ;P
Char *p, *q;
Char *packed;
Char *field[NUM_FIELDS]; // NUM_FIELDS is a define
somewhere...
UInt32 packed_size = 0;
UItn16 i; // current field (had to be
short)
// First pass, used to allocate the packed string.
for ( i = 0, i < NUM_FIELDS; i++ )
packed-size += StrLen(field[i]) + 1; // with null at the end
// Allocate the packed string.
packed = (Char *)MemPtrNew(sizeof(Char) * packed_size);
p = packed; // packed running pointer
for ( i = 0; i < NUM_FIELDS; i++ ) {
q = field[i]; // field running pointer
while ( *q != '\0' ) {
*p++ = *q++; // copy the string
} *p++ = 0;
}
After the above, 'packed' should contain the packed string, including final
terminating NULL, ready to be DmWritten someplace. And yes, in my
original, I made a glaring mistake... used the & character in place of the *
every time I referenced a pointer. That would have been bad.
- Matthew
--
For information on using the Palm Developer Forums, or to unsubscribe, please see
http://www.palmos.com/dev/support/forums/