基础知识
位 & 字节
计算机存储的是二进制(比如101010)。每一个数字就是一位 (bit), 每 8 位是一个字节 (Byte), 位是计算机传输的最小单位,而字节是编码的最小单位。
字符
每个文字都是一个字符,不同的编码对应同样字符所需的字节数也是不同的。(英文占一个字节,中文占两个字节)
编码
编码就是字节到字符的规则。(比如 0001 代表 1,也可以用 01 代表 1 这个字符)
解码
有编码就有解码,数据在网络中是二进制形式传输,加密与解密配套时可以正确解码二进制流信息。
总结
位和字节都是单位,字符是看到的结果,解码编码则是固定的规则
NSString & NSData & char
Definition
NSData
遵循 NSCopying NSCoding 协议, 它提供面向对象的数组存储为字节,即二进制数据流类型。
读写文件需要一个缓冲区,而 NSData 就提供缓存区。
Coding
char <-> NSString
逐char打印时,中文乱码是因为无法一次打印两个, 后面unicode部分有解决方法。
constchar*chars ="this is a string, and contain 中文";// char to NSStringNSString*string = [[NSStringalloc] initWithCString:chars encoding:NSUTF8StringEncoding];NSLog(@"%lu, %@", string.length, string);// 32, this is a string, and contain 中文// NSString to charchars = [string cStringUsingEncoding:NSUTF8StringEncoding];NSLog(@"%lu, %s", strlen(chars), chars);// 36, this is a string, and contain ‰∏≠Êñá
char -> NSData
NSData*dataWithBytes = [NSDatadataWithBytes:chars length:strlen(chars)];NSLog(@"%@", dataWithBytes);// <74686973 20697320 61204320 73747269 6e672c20 616e6420 636f6e74 61696e20 e4b8ade6 9687>
NString <-> NSData
NSString*string =@"this is a string, and contain 中文";// NSString to dataNSData*data = [string dataUsingEncoding:NSUTF8StringEncoding];NSLog(@"%@", data);// <74686973 20697320 61207374 72696e67 2c20616e 6420636f 6e746169 6e20e4b8 ade69687>// NSData to NSStringstring = [[NSStringalloc] initWithData:data encoding:NSUTF8StringEncoding];NSLog(@"%@", string);// this is a string, and contain 中文
NSString <-> ASCII
// NSString to int ASCIINSString*charString =@"A";intasciiCode = [charString characterAtIndex:0];NSLog(@"%i", asciiCode);// 65// int to NSStringasciiCode =66; charString = [NSStringstringWithFormat:@"%c", asciiCode];NSLog(@"%@", charString);// B
char <-> ASCII
// char to int ASCIIcharcharacter ='A';intasciiCode = (int)character;NSLog(@"%i", asciiCode);// 65// int to charasciiCode =66; character = (char)asciiCode;NSLog(@"%c", character);// B
unicode
%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*theString =@"g";unichartheChar = [theString characterAtIndex:0];NSString*theString1 = [NSStringstringWithFormat:@"%c", theChar];NSString*theString2 = [NSStringstringWithFormat:@"%C", theChar];NSLog(@"%@, %d, %@, %@",theString, theChar, theString1, theString2);// g, 103, g, gtheString =@"家"; theChar = [theString characterAtIndex:0]; theString1 = [NSStringstringWithFormat:@"%c", theChar]; theString2 = [NSStringstringWithFormat:@"%C", theChar];NSLog(@"%@, %d, %@, %@",theString, theChar, theString1, theString2);// 家, 23478, ¶, 家
参考
How to convert ASCII value to a character in Objective-C?
文/俊杰li(简书作者)
原文链接:http://www.jianshu.com/p/83acc53d459f
著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。