NSString 到 const char * 转换为希腊字符

NSString to const char * convert with Greek characters(NSString 到 const char * 转换为希腊字符)

本文介绍了NSString 到 const char * 转换为希腊字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将包含希腊语单词的 NSString 转换为 const char.我正在尝试使用用于希腊语言的 UTF-8 编码转换字符串,当我将字符注销时,它里面有垃圾.请在这里提供一点帮助..

I'm trying to convert an NSString which contains a greek word to a const char. I'm trying to convert the string with UTF-8 encoding which is for greek language and when i'm logging the char out, it has junk in it. Please a little help here..

//this is the greek word 
NSString *letter = textFieldLetter.text;

//NSString to const char conversion for the sql query
const char *cLetter = (const char *)[letter cStringUsingEncoding:NSUTF8StringEncoding]; 

//this contains junk!
NSLog(@"letter: %s",cLetter);

推荐答案

你不必使用 UTF8,而是使用 Unicode (NSUnicodeStringEncoding).

You don't have to use UTF8, instead use Unicode (NSUnicodeStringEncoding).

如果你看到 NSStringEncoding 会看到这个选项(有更多):

If you see the NSStringEncoding will see this options (have more):

enum {
NSWindowsCP1253StringEncoding = 13,    /* Greek */
NSUTF16StringEncoding = NSUnicodeStringEncoding,   

NSUTF16BigEndianStringEncoding = 0x90000100,          /* NSUTF16StringEncoding encoding with explicit endianness specified */
NSUTF16LittleEndianStringEncoding = 0x94000100,       /* NSUTF16StringEncoding encoding with explicit endianness specified */

NSUTF32StringEncoding = 0x8c000100,                   
NSUTF32BigEndianStringEncoding = 0x98000100,          /* NSUTF32StringEncoding encoding with explicit endianness specified */
NSUTF32LittleEndianStringEncoding = 0x9c000100        /* NSUTF32StringEncoding encoding with explicit endianness specified */
};
typedef NSUInteger NSStringEncoding;

您可以看到特定的希腊语:NSWindowsCP1253StringEncoding

You can see a specific Greek lenguage: NSWindowsCP1253StringEncoding

如果不行,可以尝试使用其他的UTF16或UTF32.但我认为 Unicode 适合你.

If is don't work, you can try to use other UTF16 or UTF32. But I think Unicode work for you.

--编辑

请记住,NSLog(@"%s",string); 无法解码 unicode 字符.所以,它真的会显示混乱.

Take in mind that NSLog(@"%s",string); can not decode a unicode character. So, it will really show the mess.

要测试它是否有效,请进行转换,然后重做.如果相同,则不会丢失数据.

To test if it work, do the conversion, and redo it. If is the same, so you don't lost data.

const char *cLetter = (const char *)[letter cStringUsingEncoding:NSUnicodeStringEncoding]; 
NSString *original = [NSString stringWithCString:cLetter encoding:NSUnicodeStringEncoding];

这篇关于NSString 到 const char * 转换为希腊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:NSString 到 const char * 转换为希腊字符

基础教程推荐