将NSString转换为const char * [英] convert NSString to const char *

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

问题描述

我想将nsstring值转换为const char *。

I want to convert the nsstring value into const char *.

NSString *s = @"יככעימבבגיננימ"; //Hebrew characters
const char *t = [s cStringUsingEncoding:NSUTF8StringEncoding];
NSLog(@"\n str = %s",t);

控制台显示如下◊ô◊õ◊õ◊¢◊◊◊û◊◊◊ ë◊í◊ô◊†◊†◊ô◊û。

The console showed like this "יככעימבבגיננימ".

如何将实际的NSString值转换为const char *?

How to get the actual NSString value into const char*?

谢谢,
loganathan

Thanks, loganathan

推荐答案

这只是因为%s in NSLog 表示ASCII字符串(准确地说是普通C 8位字符串),而不是UTF8。

It's just because %s in NSLog denotes ASCII string (plain-C 8 bit strings, to be precise), not UTF8.

尝试以下操作怎么样?:

What about trying the following?:

NSString *s = @"יככעימבבגיננימ"; //Hebrew characters
NSLog(@"\n str = %@",s);

您还可以注销UTF16数组:

You can log out UTF16 array, also:

const wchar_t *t = (const wchar_t*)[s cStringUsingEncoding:NSUTF16StringEncoding];
NSLog(@"\n str2 = %S",t);

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

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