开发者

Testing wchar_t* for convertable characters

开发者 https://www.devze.com 2023-03-31 15:39 出处:网络
I\'m working on talking to a library that handles strings as wchar_t arrays. I need to convert these to char arrays so that I can hand them over to Python (using SWIG and Python\'s PyString_FromString

I'm working on talking to a library that handles strings as wchar_t arrays. I need to convert these to char arrays so that I can hand them over to Python (using SWIG and Python's PyString_FromString function). Obviously not all wide characters can be converted to chars. According to the documentation for wcstombs, I ough开发者_开发问答t to be able to do something like

wcstombs(NULL, wideString, wcslen(wideString))

to test the string for unconvertable characters -- it's supposed to return -1 if there are any. However, in my test case it's always returning -1. Here's my test function:

void getString(wchar_t* target, int size) {
    int i;
    for(i = 0; i < size; ++i) {
        target[i] = L'a' + i;
    }
    printf("Generated %d characters, nominal length %d, compare %d\n", size, 
            wcslen(target), wcstombs(NULL, target, size));
}    

This is generating output like this:

Generated 32 characters, nominal length 39, compare -1
Generated 16 characters, nominal length 20, compare -1
Generated 4 characters, nominal length 6, compare -1

Any idea what I'm doing wrong?

On a related note, if you know of a way to convert directly from wchar_t*s to Python unicode strings, that'd be welcome. :) Thanks!


Clearly, as you found, it's essential to zero-terminate your input data.

Regarding the final paragraph, I would convert from wide to UTF8 and call PyUnicode_FromString.

Note that I am assuming you are using Python 2.x, it's presumably all different in Python 3.x.

0

精彩评论

暂无评论...
验证码 换一张
取 消