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 * 转换为希腊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Why local notification is not firing for UNCalendarNotificationTrigger(为什么没有为UNCalendarNotificationTrigger触发本地通知)
iOS VoiceOver functionality changes with Bundle Identifier(IOS画外音功能随捆绑包标识符而变化)
tabbar middle tab out of tabbar corner(选项卡栏中间的选项卡角外)
Pushing UIViewController above UITabBar(将UIView控制器推送到UITabBar上方)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Get an event when UIBarButtonItem menu is displayed(显示UIBarButtonItem菜单时获取事件)