objective-c - char / NSData / NSString 

基础知识

  • 位 & 字节

    计算机存储的是二进制(比如101010)。每一个数字就是一位 (bit), 每 8 位是一个字节 (Byte), 位是计算机传输的最小单位,而字节是编码的最小单位。

  • 字符

    每个文字都是一个字符,不同的编码对应同样字符所需的字节数也是不同的。(英文占一个字节,中文占两个字节)

  • 编码

    编码就是字节到字符的规则。(比如 0001 代表 1,也可以用 01 代表 1 这个字符)

  • 解码

    有编码就有解码,数据在网络中是二进制形式传输,加密与解密配套时可以正确解码二进制流信息。

  • 总结

    位和字节都是单位,字符是看到的结果,解码编码则是固定的规则

NSString & NSData & char

Definition

  • NSData

    • 遵循 NSCopying NSCoding 协议, 它提供面向对象的数组存储为字节,即二进制数据流类型。
    • 读写文件需要一个缓冲区,而 NSData 就提供缓存区

Coding

  • char <-> NSString

    char 打印时,中文乱码是因为无法一次打印两个, 后面 unicode 部分有解决方法。

      const char *chars = "this is a string, and contain 中文";
    
      // char to NSString
      NSString *string = [[NSString alloc] initWithCString:chars encoding:NSUTF8StringEncoding];
      NSLog(@"%lu, %@", string.length, string); // 32, this is a string, and contain 中文
    
      // NSString to char
      chars = [string cStringUsingEncoding:NSUTF8StringEncoding];
      NSLog(@"%lu, %s", strlen(chars), chars); // 36, this is a string, and contain 中文
    
  • char -> NSData

      NSData *dataWithBytes = [NSData dataWithBytes: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 data
      NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
      NSLog(@"%@", data); // <74686973 20697320 61207374 72696e67 2c20616e 6420636f 6e746169 6e20e4b8 ade69687>
    
      // NSData to NSString
      string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
      NSLog(@"%@", string); // this is a string, and contain 中文
    
  • NSString <-> ASCII

      // NSString to int ASCII
      NSString *charString = @"A";
      int asciiCode = [charString characterAtIndex:0];
      NSLog(@"%i", asciiCode); // 65
    
      // int to NSString
      asciiCode = 66;
      charString = [NSString stringWithFormat:@"%c", asciiCode];
      NSLog(@"%@", charString); // B
    
  • char <-> ASCII

      // char to int ASCII
      char character = 'A';
      int asciiCode = (int)character;
      NSLog(@"%i", asciiCode); // 65
    
      // int to char
      asciiCode = 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";
      unichar theChar = [theString characterAtIndex:0];
      NSString *theString1 = [NSString stringWithFormat:@"%c", theChar];
      NSString *theString2 = [NSString stringWithFormat:@"%C", theChar];
      NSLog(@"%@, %d, %@, %@",theString, theChar, theString1, theString2);
      // g, 103, g, g
    
      theString = @"家";
      theChar = [theString characterAtIndex:0];
      theString1 = [NSString stringWithFormat:@"%c", theChar];
      theString2 = [NSString stringWithFormat:@"%C", theChar];
      NSLog(@"%@, %d, %@, %@",theString, theChar, theString1, theString2);
      // 家, 23478, ¶, 家
    

参考

    原文作者:二石兄
    原文地址: https://www.jianshu.com/p/83acc53d459f
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞