在C使用UTF8字符串 [英] Using utf8 strings in C

查看:352
本文介绍了在C使用UTF8字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个原生扩展到Adobe AIR C中的code应该被移植到其他平台的版本。 在我的C面功能我得到的字符串从空中像这样

I'm writing a native extension to Adobe AIR in C. The code should be ported to other platforms later. In my function on C side I'm getting a string from air like this

uint32_t len;
const uint8_t * str = 0;
if( FRE_OK == FREGetObjectAsUTF8(argv[0], &len, &str) )
{
    //Here i need to pass a string as an argument to other function
    printf("Got string %s", str); //Showing weird letters instead of str
}

FREGetObjectAsUTF8返回一个UTF8连接$​​ C $ CD字符串,它应该重新psented为const uint8_t有$ P $。我工作在MacOS和X code和uint8_t有被定义为unsigned char型。问题是在一组C code这需要一个简单的char *作为参数。我不需要从单code任意的字母,我只用拉丁文字母和数字。

FREGetObjectAsUTF8 returns a UTF8 encoded string which should be represented as const uint8_t. I'm working in MacOS and XCode and uint8_t is defined as unsigned char. The problem is in a bunch of c code which expects a simple char* as argument. I don't need any letters from unicode and I'm using only latin letters and digits.

我试着投一个类型,没有运气。作为例子

I've tried to cast a type with no luck. As example

char buffer[512];
sprintf(buffer, "%s", (char*)str); //Same weird letters here

但是,如果我遍历字符串,我得到正确的值

But if I iterate over string I'm getting correct value

for(i=0; i<len; i++)
    printf("%s", str[i]); // Normal value

所以亩的问题是:我怎么能传递一个UTF8字符串的函数需要一个简单的符号字符? 在实际上,我可以尝试在C ++创建函数和使用C部分,与外部,但纯C的解决方案会更preferable。

So mu question is: How could I pass a utf8 string to a function that expects a simple signed char? In a fact I could try to create functions in c++ and use C part with "extern" but pure C solution will be more preferable.

我传递字符串initapp空气,如果我回到它回到运行时,它显示我正确的值initapp。在我的C code我想通过它在功能上它期望的char *为参数

I'm passing string "initapp" from air and if I return it back to runtime it shows me correct value "initapp". In my C code I'm trying to pass it in function which expects char* as an argument

FREObject initApp(FREContext ctx, void* funcData, uint32_t argc, FREObject argv[])
{
    uint32_t len;
    const uint8_t * str = 0;
    if( FRE_OK == FREGetObjectAsUTF8(argv[0], &len, &str) )
    {
        /*
        I have about 40 functions and most of them working with ASCII strings
        */
        executeCommand( (const char*)str );
        FREObject result;
        FRENewObjectFromUTF8(len, str, &result);
        return result; //It's ok. Correct string
    }
    return NULL;
}

但在我的功能,而不是initapp我收到的各种怪异的字母(每次都不同),喜欢尝试输出图像或不正确的变量的某些部分。

But in my function instead of "initapp" I'm getting various weird letters(different each time) like trying to output some part of image or incorrect variable.

任何帮助将是非常美联社preciated。

Any help will be highly appreciated.

推荐答案

Mac OS X的普遍预期简单的char *字符串是UTF-8,无论如何,所以你应该得到正确的结果与code您呈现。

Mac OS X generally expects simple char* strings to be UTF-8 anyway, so you should be getting correct results with the code you showed.

sprintf(buffer, "%s", (char*)str);

如果code像下面这样打印出的数值表示有效的UTF-8字符串:

If code like the following prints out numeric values indicating a valid UTF-8 string:

if( FRE_OK == FREGetObjectAsUTF8(argv[0], &len, &str) ) {
    for(int i=0; i<len; ++i)
        printf("0x%02X ", str[i]);

    FREObject result;
    FRENewObjectFromUTF8(len, str, &result);
}

和通过调用自己的函数产生的垃圾更换的printf循环:

And replacing the printf loop with calls to your own functions results in garbage:

if( FRE_OK == FREGetObjectAsUTF8(argv[0], &len, &str) ) {
    executeCommand( (const char*)str );

    FREObject result;
    FRENewObjectFromUTF8(len, str, &result);
}

还有最有可能与的ExecuteCommand()的一个问题。

there is most likely a problem with executeCommand().

这篇关于在C使用UTF8字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆