NSString to const char * convert with Greek characters(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 * 转换为希腊字符
基础教程推荐
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01