I am having problems disecting a string.  I am using the TxtGetChar function
inside a for loop to build a smaller string of a set length to pass to an
encryption function.  This returns a WChar.  When I concatenate the returned
WChar cast to a char or char* I get the char and a '/1' character.  I
presume that this is the second byte of the 2 bytes in the WChar.

Casting a WChar (16-bit value) to a Char obviously tosses the most significant byte, and thus won't work with Japanese and other wide character encodings. The inverse of TxtGetChar (or TxtGetNextChar) is TxtSetNextChar.


Is there a way of converting from a WChar to a single byte char ?  And
should I be using char or Char ?  I have been using char as it seems to give
more consistent results, but not much

If you need to truncate a string to some max length, then either use TxtGetTruncationOffset (in place) or StrNCopy (if copying), as in.


        Char sourceStr[<long length>];
        <sourceStr gets filled with bytes somehow>

        UInt16 offset = TxtGetTruncationOffset(sourceStr, <shorter length>);
        sourceStr[offset] = '\0';

or

        StrNCopy(shortStr, sourceStr, <shorter length>);
        shortStr[<shorter length>] = '\0';

If you did want to copy character by character in a loop, then you'd want to use TxtSetNextChar to append each WChar to the destination string, as in:

        UInt16 srcOffset = 0;
        UInt16 dstOffset = 0;

UInt16 maxOffset = StrLen(sourceStr);

        while (srcOffset < maxOffset)
        {
                WChar curChar;
                srcOffset += TxtGetNextChar(sourceStr, srcOffset, &curChar);

                dstOffset += TxtSetNextChar(shortStr, dstOffset, curChar);
        }

-- Ken
--
Ken Krugler
TransPac Software, Inc.
<http://www.transpac.com>
+1 530-470-9200

--
For information on using the Palm Developer Forums, or to unsubscribe, please see 
http://www.palmos.com/dev/support/forums/

Reply via email to