iOS-字节数组Byte、Char、NSData、NSString

一:字节-Byte

1个字 =2个字节 = 16 bit
1Byte =1个字节 = 8 bit(1B = 1根数据总线= 1个字节 )
1Byte = 8 bit (8个二进制位 0000 0000,即8个bit位,8位1个字节)
1B
1KB == 1024 Byte = 2 ^ 10 (10根地址总线) 00 0000 0000 ~ 11 1111 1111
1MB == 1024 * 1024 Byte = 2 ^ 20 (20根地址总线) 0000 0000 0000 0000 0000 ~ 1111 1111 1111 1111 1111

详情请看这里:地址总线-数据总线

“ 32位cup 对应的字节大小”
数据类型 储存空间大小(单位:字节) 范围
Byte(字节型) 1 0 - 255
Boolean(布尔型/逻辑型) 2 True 或 False
Integer(整数型) 2 -32,768 ~ 32767
Byte byte[] = {1,2,3,-255}; 
NSData *headerData = [[NSData alloc] initWithBytes:byte length:4];

字节数组 byte[],这里有4个字节,每个字节8个二进制位(即:0000 0000~1111 1111,2^8 = 256)
每个字节可存放最大的数是255,
字节数组

二:Byte、char、NSData、NSString 相互转换

了解:字节、字符、字符串、二进制数据

//1.1、NSData --> Byte
  NSString *testString = @"1234567890";
  NSData *testData = [testString dataUsingEncoding: NSUTF8StringEncoding];
  Byte *DataByte = (Byte *)[testData bytes];
   
//1.2、Byte --> NSData
  Byte byte[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23};
  NSData *Bytedata = [[NSData alloc] initWithBytes:byte length:24];
  NSInteger byteLength2 = sizeof(byte2);
  NSLog(@"字节长度_byteLength2= %zd",byteLength2);//byteLength2= 24
  NSLog(@"---<2.2>testByte: %s", DataByte);
  NSLog(@"---<2.2>Bytedata: %@\n ", Bytedata);
//2.1、 NSString 转化为 char*
  NSString *strr = @"Test转化123";
    const char * charA = [strr UTF8String];
  NSLog(@"charA = %s",charA);

//2.2、char* 转化为 NSString
  NSString *charToString = [NSString stringWithUTF8String:charA];
  NSLog(@"charToString= %@ \n ",charToString);
//3.1、:NSString --> NSData
    NSString * str1 = @"hello, world!";
    NSData * data = [str1 dataUsingEncoding:NSUTF8StringEncoding];
    
//3.2、NSData --> NSString
    NSString * dataString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    NSLog(@"---<2.1>data.bytes: %s", data.bytes);
    NSLog(@"---<2.1>dataString: %@\n ", dataString);


三:NSMutableData 的拼接、删除、插入

    NSData *strData = [@"testdata" dataUsingEncoding:NSUTF8StringEncoding];
    NSMutableData *data = [NSMutableData data];

    [data appendData:strData];
    NSLog(@"data length= %ld",data.length);
    
    // delete
    [data replaceBytesInRange:NSMakeRange(0, 4) withBytes:NULL length:0];
    NSLog(@"data length= %ld",data.length);
    
    // insert
    [data replaceBytesInRange:NSMakeRange(0, 0) withBytes:strData.bytes length:strData.length];
    NSLog(@"data length= %ld",data.length);    
}


四:socket 组装发送消息

- (void)zmTest {
    
    //NSData-> Byte数组
    // 消息正文
    NSString *contentStr = @"abcdEFG";
    for (int i=0; i<10; i++) {
        contentStr = [contentStr stringByAppendingString:@"1234567890-"];
    }
    //NSLog(@"contentStr= %@ ",contentStr);
    NSData *contentData = [contentStr dataUsingEncoding: NSUTF8StringEncoding];    
    NSLog(@"contentStr_长度= %zd ",contentStr.length);
    //计算正文长度
  NSUInteger a1 = contentStr.length%256;   //0~255   
  NSUInteger a2 = contentStr.length%16777216%65536/256;  //256~65535
  NSUInteger a3 = contentStr.length%16777216/65536;    //65536~16777215
  NSUInteger a4 = contentStr.length/16777216;     //16777216~4294967295

    NSLog(@"(a1,a2,a3,a4) = (%zd,%zd,%zd,%zd) ",a1,a2,a3,a4);
    
    // 消息头
    Byte byte[] = {a4,a3,a2,a1}; //4个字节
    NSData *headerData = [[NSData alloc] initWithBytes:byte length:4];
        
    // 消息组装体(总消息、总长度) 
    NSMutableData *mutData = [[NSMutableData alloc] init];
    [mutData appendData:headerData];
    [mutData appendData:contentData];
    

//------以下为测试------
    NSLog(@"测试:1、消息正文 ------------------------------- ");
    Byte *contentByte = (Byte *)[contentData bytes];
    for(int i=0;i<[contentData length];i++){
        printf("体_contentByte = %d\n",contentByte[i]);
    }

    NSLog(@"测试:2、消息头------------------------------- ");
    Byte *headerByte = (Byte *)[headerData bytes];
    for(int i=0;i<[headerData length];i++){
        printf("头_headerByte = %d\n",headerByte[i]);
    }

    NSLog(@"测试:3、总消息------------------------------- ");
    NSString *ByteString = @"[";
    Byte *mutByte = (Byte *)[mutData bytes];
    for(int i=0;i<[mutData length];i++){
        //printf("mutByte = %d\n",mutByte[i]);
        ByteString = [NSString stringWithFormat:@"%@%d,",ByteString,mutByte[i]];
    }
    NSLog(@"---All:ByteString_1= %@\n ", ByteString);
    NSLog(@"---mutData.length_11= %zd\n ", mutData.length);
    NSLogline(@" ");

    //截取数据  
    [mutData subdataWithRange:NSMakeRange(0, 110)];//删除索引0到索引50的数据 
    NSLog(@"---mutData.length_22= %zd ", mutData.length);
    NSLog(@"---[mutData length]_22= %zd ", [mutData length]);
    Byte *mutByte2 = (Byte *)[mutData bytes];
    NSString *ByteString2 = @"[";
    for(int i=0;i<[mutData length];i++){
        if (i==([mutData length]-1)) {
            ByteString2 = [NSString stringWithFormat:@"%@%d]",ByteString2,mutByte2[i]];
        }else{
            ByteString2 = [NSString stringWithFormat:@"%@%d,",ByteString2,mutByte2[i]];
        }
    }
    NSLog(@"---截取:ByteString_2= %@ ", ByteString2);
    NSLog(@"---mutData.length_22= %zd \n ", mutData.length);
    
    
    //删除数据  
    [mutData replaceBytesInRange:NSMakeRange(0, 117) withBytes:NULL length:0];//删除索引0到索引50的数据 
    NSLog(@"---mutData.length_33= %zd ", mutData.length);
    NSLog(@"---[mutData length]_33= %zd ", [mutData length]);
    Byte *mutByte3 = (Byte *)[mutData bytes];
    NSString *ByteString3 = @"[";
    for(int i=0;i<[mutData length];i++){
        if (i==([mutData length]-1)) {
            ByteString3 = [NSString stringWithFormat:@"%@%d]",ByteString3,mutByte3[i]];
        }else{
            ByteString3 = [NSString stringWithFormat:@"%@%d,",ByteString3,mutByte3[i]];
        }
    }
    NSLog(@"---删除:ByteString_3= %@ ", ByteString3);
    NSLog(@"---mutData.length_33= %zd \n ", mutData.length);
  

你可能感兴趣的:(iOS-字节数组Byte、Char、NSData、NSString)