[Python-Dev] getting the UCS-2 representation of a unicode object

Fredrik Lundh fredrik@pythonware.com
Sun, 19 May 2002 17:23:28 +0200


andreas wrote:


> Sounds reasonable..but since Py_ParseTuple() only applies to function
> arguments it can not be used to convert a unicode object to UCS-2.
> So what is the easiest way to get the UCS-2 representation?
> PyUnicode_AS_DATA() returns for u'computer' a char * with strlen()==1,
> however PyUnicode_GET_DATA_SIZE() on the same string returns 16
> (looks fine for the two byes encoding of UCS-2).

strlen() looks for the first null byte.  in a UCS-2 string containing
ASCII data, every second byte will be a null byte.

> Am I missing something?

trust the macros, and don't use 8-bit functions on 16-bit strings.

</F>