Unicode problem in ucs4

M.-A. Lemburg mal at egenix.com
Mon Mar 23 17:37:12 EDT 2009


On 2009-03-23 12:57, abhi wrote:
>>> Is there any way
>>> by which I can force wchar_t to be 2 bytes, or can I convert this UCS4
>>> data to UCS2 explicitly?
>> Sure: just use the appropriate UTF-16 codec for this.
>>
>> /* Generic codec based encoding API.
>>
>>    object is passed through the encoder function found for the given
>>    encoding using the error handling method defined by errors. errors
>>    may be NULL to use the default method defined for the codec.
>>
>>    Raises a LookupError in case no encoder can be found.
>>
>>  */
>>
>> PyAPI_FUNC(PyObject *) PyCodec_Encode(
>>        PyObject *object,
>>        const char *encoding,
>>        const char *errors
>>        );
>>
>> encoding needs to be set to 'utf-16-le' for little endian, 'utf-16-be'
>> for big endian.
> 
> Thanks, but this is returning PyObject *, whereas I need value in some
> variable which can be printed using wprintf() like wchar_t (having a
> size of 2 bytes). If I again convert this PyObject to wchar_t or
> PyUnicode, I go back to where I started. :)

It will return a PyString object with the UTF-16 data. You can
use PyString_AS_STRING() to access the data stored by it.

Note that writing your own UCS2/UCS4 converter isn't all that hard
either. Just have a look at the code in unicodeobject.c for
PyUnicode_AsWideChar().

-- 
Marc-Andre Lemburg
eGenix.com

Professional Python Services directly from the Source  (#1, Mar 23 2009)
>>> Python/Zope Consulting and Support ...        http://www.egenix.com/
>>> mxODBC.Zope.Database.Adapter ...             http://zope.egenix.com/
>>> mxODBC, mxDateTime, mxTextTools ...        http://python.egenix.com/
________________________________________________________________________
2009-03-19: Released mxODBC.Connect 1.0.1      http://python.egenix.com/

::: Try our new mxODBC.Connect Python Database Interface for free ! ::::


   eGenix.com Software, Skills and Services GmbH  Pastor-Loeh-Str.48
    D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
           Registered at Amtsgericht Duesseldorf: HRB 46611
               http://www.egenix.com/company/contact/



More information about the Python-list mailing list