将带有重音字符的NSString转换为CString [英] converting an NSString with accented characters to a CString

查看:114
本文介绍了将带有重音字符的NSString转换为CString的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个值为Jose的NSString(e上的口音)。我尝试将其转换为C字符串,如下所示:

I have an NSString with a value of Jose (an accent on the e). I try to convert it to a C string as follows:

char str [[myAccentStr length] + 1];
[myAccentStr getCString:str maxLength:[myAccentStr length] + 1 encoding:NSUTF32StringEncoding];

但是str最后是一个空字符串。是什么赋予了?我也尝试过UTF8和UTF16。它后来被传递给另一个函数,当该函数调用lstrlen时,它的大小就是零。

but str ends up being an empty string. What gives? I tried UTF8 and UTF16 too. It gets passed to another function later on and when that funcsion calls lstrlen on it, the size comes out as zero.

推荐答案

docs for NSString getCString:maxLength:encoding 说:

The docs for NSString getCString:maxLength:encoding says:


可以使用canBeConvertedToEncoding:字符串可以是
无损转换为编码。如果不能,您可以使用
dataUsingEncoding:allowLossyConversion:使用编码获取C字符串
表示,允许某些信息丢失(请注意
dataUsingEncoding返回的数据:allowLossyConversion :是
不是一个严格的C字符串,因为它没有NULL终止符)。

You can use canBeConvertedToEncoding: to check whether a string can be losslessly converted to encoding. If it can’t, you can use dataUsingEncoding:allowLossyConversion: to get a C-string representation using encoding, allowing some loss of information (note that the data returned by dataUsingEncoding:allowLossyConversion: is not a strict C-string since it does not have a NULL terminator).

使用NSString方法< a href =http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html#//apple_ref/doc/uid/20000154-dataUsingEncoding_ =nofollow> dataUsingEncoding:allowLossyConversion:做的伎俩。这是一个代码示例:

Using the NSString method dataUsingEncoding:allowLossyConversion: does the trick. Here's a code example:

NSString *myAccentStr = @"José";
char str[[myAccentStr length] + 1];

// NSString * to C String (char*)
NSData *strData = [myAccentStr dataUsingEncoding:NSMacOSRomanStringEncoding 
                                allowLossyConversion:YES];
memcpy(str, [strData bytes], [strData length] + 1);
str[[myAccentStr length]] = '\0';
NSLog(@"str (from NSString* to c string): %s", str);

// C String (char*) to NSString *   
NSString *newAccentStr = [NSString stringWithCString:str 
                                            encoding:NSMacOSRomanStringEncoding];
NSLog(@"newAccentStr (from c string to NSString*):  %@", newAccentStr);

该NSLog的输出是:

The output from that NSLog is:


str(从NSString *到c字符串):José

str (from NSString* to c string): José

newAccentStr(从c字符串到NSString *):José

newAccentStr (from c string to NSString*): José

到目前为止,我在使用NSMacOSRomanStringEncoding时才正确看到这个工作。

So far I've only seen this work properly when using the NSMacOSRomanStringEncoding.

将其更改为社区wiki。请随时编辑。

Changing this to a community wiki. Please feel free to edit.

hooleyhoop有一些好点,所以我以为我会尽量使代码尽可能冗长。如果我没有任何东西,有人请进来。

hooleyhoop had some great points, so I thought I would try to make code that is as verbose as possible. If I'm missing anything, someone please chime in.

另外 - 不知道为什么[NSString canBeConvertedToEncoding:]返回YES即使[NSString getCString:maxLength:encoding :]函数绝对不能正常工作(如输出所示)

Also - Not sure why [NSString canBeConvertedToEncoding:] is returning YES even though the [NSString getCString:maxLength:encoding:] function definitely isn't working right (as seen by the output).

这里有一些代码来帮助分析什么是有用的/什么不是:

Here's some code to help in analyzing what works / what doesn't:

// Define Block variable to tests out different encodings
void (^tryGetCStringUsingEncoding)(NSString*, NSStringEncoding) = ^(NSString* originalNSString, NSStringEncoding encoding) {
    NSLog(@"Trying to convert \"%@\" using encoding: 0x%X", originalNSString, encoding);
    BOOL canEncode = [originalNSString canBeConvertedToEncoding:encoding];
    if (!canEncode)
    {
        NSLog(@"    Can not encode \"%@\" using encoding %X", originalNSString, encoding);
    }
    else
    {
        // Try encoding using NSString getCString:maxLength:encoding:
        NSUInteger cStrLength = [originalNSString lengthOfBytesUsingEncoding:encoding];
        char cstr[cStrLength];
        [originalNSString getCString:cstr maxLength:cStrLength encoding:encoding];
        NSLog(@"    Converted(1): \"%s\"  (expected length: %u)",
              cstr, cStrLength);

        // Try encoding using NSString dataUsingEncoding:allowLossyConversion:          
        NSData *strData = [originalNSString dataUsingEncoding:encoding allowLossyConversion:YES];
        char cstr2[[strData length] + 1];
        memcpy(cstr2, [strData bytes], [strData length] + 1);
        cstr2[[strData length]] = '\0';
        NSLog(@"    Converted(2): \"%s\"  (expected length: %u)",
              cstr2, [strData length]);
    }
};

NSString *myAccentStr = @"José";

// Try out whatever encoding you want
tryGetCStringUsingEncoding(myAccentStr, NSUTF8StringEncoding);
tryGetCStringUsingEncoding(myAccentStr, NSUTF16StringEncoding);
tryGetCStringUsingEncoding(myAccentStr, NSUTF32StringEncoding);
tryGetCStringUsingEncoding(myAccentStr, NSMacOSRomanStringEncoding);

结果:

> Trying to convert "José" using encoding: 0x4
>     Converted(1): ""  (expected length: 5)
>     Converted(2): "José"  (expected length: 5)
> Trying to convert "José" using encoding: 0xA
>     Converted(1): ""  (expected length: 8)
>     Converted(2): "ˇ˛J"  (expected length: 10)
> Trying to convert "José" using encoding: 0x8C000100
>     Converted(1): ""  (expected length: 16)
>     Converted(2): "ˇ˛"  (expected length: 20)
> Trying to convert "José" using encoding: 0x1E
>     Converted(1): "-"  (expected length: 4)
>     Converted(2): "José"  (expected length: 4)

这篇关于将带有重音字符的NSString转换为CString的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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