博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
中文字符ASCII码和NSString相互转换
阅读量:4296 次
发布时间:2019-05-27

本文共 2179 字,大约阅读时间需要 7 分钟。

在xcode中,文件以utf8格式保存。因此,其中变量对象也是以utf8格式保存。不同语言的utf8编码不一样,英文的utf8编码和ascii码一样。

不同语言的每个字符的utf8编码的字节数不一样,字节码也不一样。对于英文字符,查看它的ascii码,很方便,将字符取出来,就是它的ascii码。其实,对于非英文字符,取字符集编码的方式也是这样。这样统称为取ASCII码,在很多文档中也是这样描述的。
网上很多这样例子,介绍如何将字符和ASCII码相互转化。但是它们都没有提及如何转换中文等其他非英文的字符,使用这个方法都会转成乱码。
 
使用英文转换测试,如下所示:
// NSString to ASCII
NSString *string = @"A";
int asciiCode = [string characterAtIndex:0]; // 65
 
// ASCII to NSString
int asciiCode = 65;
NSString *string = [NSString stringWithFormat:@"%c", asciiCode]; // A
 
再使用中文测试一下,使用[NSString stringWithFormat:@"%c", asciiCode]得到的是乱码字符,就是说根本没识别正确。
再说解决方法之前,先了解一下stringWithFormat方法中各种format。其中将ascii码转成字符有两种format,分别为%c和%C。
 
    /*
     %c
     8-bit unsigned character (unsigned char), printed by NSLog() as an ASCII character, or, if not an ASCII character, in the octal format //ddd or the Unicode hexadecimal format //udddd, where d is a digit.
     %C
     16-bit Unicode character (unichar), printed by NSLog() as an ASCII character, or, if not an ASCII character, in the octal format //ddd or the Unicode hexadecimal format //udddd, where d is a digit.
     */
使用[NSString stringWithFormat:@"%C", asciiCode]就可以正常得到所要的字符。
分别以英文,中文和日文举例。
 
    NSString *theString = @"g";
    unichar theChar = [theString characterAtIndex:0];
    NSString *theString1 = [NSString stringWithFormat:@"%c", theChar];
    NSString *theString2 = [NSString stringWithFormat:@"%C", theChar];
    NSLog(@"theString=%@,%d,%@,%@",theString,theChar,theString1,theString2);
    
    theString = @"家";
    theChar = [theString characterAtIndex:0];
    theString1 = [NSString stringWithFormat:@"%c", theChar];
    theString2 = [NSString stringWithFormat:@"%C", theChar];
    NSLog(@"theString=%@,%d,%@,%@",theString,theChar,theString1,theString2);
    
    theString = @"カントリ`";
    theChar = [theString characterAtIndex:2];
    theString1 = [NSString stringWithFormat:@"%c", theChar];
    theString2 = [NSString stringWithFormat:@"%C", theChar];
    NSLog(@"theString=%@,%d,%@,%@",theString,theChar,theString1,theString2);
 
2013-09-12 15:36:27.849 XYShopping[1892:18e03] theString=g,103,g,g
2013-09-12 15:36:27.849 XYShopping[1892:18e03] theString=家,23478,?,家
2013-09-12 15:36:27.849 XYShopping[1892:18e03] theString=カントリ`,12488,?,ト
 
显示结果表明,这个方法是正确的。对于两个字节组成的字符,是能显示出的。不知道其他语言会怎么样,没有条件去测试。

转载地址:http://nkdws.baihongyu.com/

你可能感兴趣的文章
iOS在Xcode6中怎么创建OC category文件
查看>>
Expanding User-Defined Runtime Attributes in Xcode with Objective-C
查看>>
iOS7 UITabBar自定义选中图片显示为默认蓝色的Bug
查看>>
提升UITableView性能-复杂页面的优化
查看>>
25 iOS App Performance Tips & Tricks
查看>>
那些好用的iOS开发工具
查看>>
iOS最佳实践
查看>>
使用CFStringTransform将汉字转换为拼音
查看>>
更轻量的 View Controllers
查看>>
Chisel-LLDB命令插件,让调试更Easy
查看>>
时间格式化hh:mm:ss和HH:mm:ss区别
查看>>
When to use Delegation, Notification, or Observation in iOS
查看>>
Objective-C Autorelease Pool 的实现原理
查看>>
编程语言大牛王垠:编程的智慧,带你少走弯路
查看>>
ios指令集以及基于指令集的app包压缩策略
查看>>
iOS开发者的福利 — — iOS9+Xcode7免越狱免证书直接调试
查看>>
3、JavaWeb学习之基础篇—JSP
查看>>
4、JavaWeb学习之基础篇—Session
查看>>
5、JavaWeb学习之基础篇—标签(自定义&JSTL)
查看>>
8、JavaWEB学习之基础篇—文件上传&下载
查看>>