ios字母与数字相互转化,ascll码转化
2016-08-24 16:06
495 查看
发个广告:ios开发两年了,一步步走来 关注公众号一起进步
NSString *string = @"A";
NSInteger int = [string characterAtIndex:0];
上面的结果胃65
int
aaa= 65;
NSString *string = [NSString stringWithFormat:@"%c",aaa]; //
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,?,ト
显示结果表明,这个方法是正确的。对于两个字节组成的字符,是能显示出的。不知道其他语言会怎么样,没有条件去测试。
NSString *string = @"A";
NSInteger int = [string characterAtIndex:0];
上面的结果胃65
int
aaa= 65;
NSString *string = [NSString stringWithFormat:@"%c",aaa]; //
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,?,ト
显示结果表明,这个方法是正确的。对于两个字节组成的字符,是能显示出的。不知道其他语言会怎么样,没有条件去测试。
相关文章推荐
- VBA实现数字列与字母列的相互转化
- FLASH中字母与数字的相互转化
- 递归问题--数字转化为字母,如(0-A,1-B....25-Z, 26-AA...)
- 标准c++下数字和字符串的相互转化
- ios 时间戳 当前时间 相互转化
- 字符串和数字的相互转化
- iOS 时间戳 与 时间相互转化
- ios 字符串和数字互相转化
- js数字和字符串相互转化
- ios 正则表达式 去标点 只含数字 字母 汉字 空格
- ios 限制只能输入字母和数字
- 字符与数字的相互转化
- 前边带0的数字字符串与纯数字的相互转化
- iOS 时间戳 与 时间相互转化
- ios 时间戳 当前时间 相互转化
- 将一个数字字符串转换为数字(支持数字中间夹杂字母,但是受int型的限制,转化的长度很有限),如:"1234" -->1234
- php中IP地址和数字相互转化
- js关键字typeof和instanceof的使用,以及js字符串与数字相互转化
- iOS 时间戳 与 时间相互转化
- ios 限制只能输入字母和数字