iOS 关于大小端以及一些数据补位

关于大小端

所谓大端数据就是数据的高字节保存在内存的低地址中,而数据的低字节保存在内存的高地址中,这样的存储模式有点儿类似于把数据当作字符串顺序处理:地址由小向大增加,而数据从高位往低位放;这和我们的阅读习惯一致。

所谓的小端数据就是数据的高字节保存在内存的高地址中,而数据的低字节保存在内存的低地址中,这种存储模式将地址的高低和数据位权有效地结合起来,高地址部分权值高,低地址部分权值低。(简单的来说就是反过来存放数据了)

关于补位

数据补位,是指在将数据按照8个字节一段进行加密或解密得到一段8个字节的密文或者明文,最后一段不足8个字节,按照需求补足8个字节(通常补00或者FF,根据实际要求不同)进行计算,之后按照顺序将计算所得的数据连在一起即可。

举个栗子!

假设要发送一段这样的数据(16进制数据):

| head | cmdlen(2字节) | cmd | playloadLen (4 字节) | playload

这段数据的内容大概是包含了:我有一个头部标记,然后用头部之后的两个字节作为描述何种命令的长度,然后接上的是何种命令,然后接上了附带信息的长度(4个字节),然后是附带信息。

需要发送的信息为:**| https(头部) | len | openStream | len2 | {@"速度与激情.h264",@"fileName"} **

//
//  CtpTools.m
//  NetworkTest
//
//  Created by 飞天江郎 on 10/11/2016.
//  Copyright © 2016 EzioChen. All rights reserved.
//

//普通字符串转换为十六进制的。

+ (NSString *)hexStringFromString:(NSString *)string{
    NSData *myD = [string dataUsingEncoding:NSUTF8StringEncoding];
    Byte *bytes = (Byte *)[myD bytes];
    //下面是Byte 转换为16进制。
    NSString *hexStr=@"";
    for(int i=0;i<[myD length];i++)
        
    {
        NSString *newHexStr = [NSString stringWithFormat:@"%x",bytes[i]&0xff];///16进制数
        
        if([newHexStr length]==1)
            
            hexStr = [NSString stringWithFormat:@"%@0%@",hexStr,newHexStr];
        
        else
            
            hexStr = [NSString stringWithFormat:@"%@%@",hexStr,newHexStr];
    }   
    return hexStr;   
}

//编写一个NSData类型数据
+(NSMutableData*)HexStringToData:(NSString*)str{
    
    NSString *command = str;
    command = [command stringByReplacingOccurrencesOfString:@" " withString:@""];
    NSMutableData *commandToSend= [[NSMutableData alloc] init];
    unsigned char whole_byte;
    char byte_chars[3] = {'\0','\0','\0'};
    int i;
    for (i=0; i < [command length]/2; i++) {
        byte_chars[0] = [command characterAtIndex:i*2];
        byte_chars[1] = [command characterAtIndex:i*2+1];
        whole_byte = strtol(byte_chars, NULL, 16);
        [commandToSend appendBytes:&whole_byte length:1];
    }
    return commandToSend;
}

//补位的方法
+(NSString*)addString:(NSString*)string Length:(NSInteger)length OnString:(NSString*)str{
    
    NSMutableString * nullStr = [[NSMutableString alloc] initWithString:@""];
    if ((length-str.length)> 0) {
        for (int i = 0; i< (length-str.length); i++) {
            [nullStr appendString:string];
        }
    }
    return [NSString stringWithFormat:@"%@%@",nullStr,str];
}

//把整型转化为16进制的,然后在补位,转换成大端数据
+(NSString *)intToHexString:(NSInteger)number length:(NSInteger)len{

    NSString *result = [self addString:@"0" Length:len OnString:[self ToHex:(unsigned int)number]];
    NSData *data = [self HexStringToData:result];
    NSData *lastData = [self dataTransfromBigOrSmall:data];
    
    result = [self dataChangeToString:lastData];
    
    return result;
    
}

//把int 变成 16进制的字符串
+(NSString *)ToHex:(unsigned int)number
{
    NSString * result = [NSString stringWithFormat:@"%x",number];
    if (result.length<2) {
        result = [NSString stringWithFormat:@"0%x",number];
    }
    return [result uppercaseString];
}

//把data(十六进制)转化为为十进制整型
+(NSInteger)dataToInt:(NSData *)data{

    
    NSInteger temp;
    double sum = 0.0;
    NSString * string = [self dataChangeToString:data];
    NSInteger length = string.length;
    
    
    for (int i = 0 ; i < length; i++) {
        NSString * tempStr = [string substringWithRange:NSMakeRange(i, 1)];
        int tempInt = [self StringToInt:tempStr];
        double tempDouble = tempInt * pow(16.0, (double)(length-1-i));
        sum = sum + tempDouble;
    }
    
    temp = (NSInteger)sum;
    return temp;

}

//大小端数据转换(其实还有更简便的方法,不过看起来这个方法是最直观的)
+(NSData *)dataTransfromBigOrSmall:(NSData *)data{

    NSString *tmpStr = [self dataChangeToString:data];
    NSMutableArray *tmpArra = [NSMutableArray array];
    for (int i = 0 ;i" withString:@""];
    string = [string stringByReplacingOccurrencesOfString:@" " withString:@""];
    return string;
    
}

+(int)StringToInt:(NSString*)string{
    
    int temp;
    
    temp = [string intValue];
    
    if ([string isEqual:@"a"]||[string isEqual:@"A"]) {
        temp = 10;
    }else if ([string isEqual:@"b"]||[string isEqual:@"B"])
    {
        temp = 11;
    }else if ([string isEqual:@"c"]||[string isEqual:@"C"])
    {
        temp = 12;
    }else if ([string isEqual:@"d"]||[string isEqual:@"D"])
    {
        temp = 13;
    }else if ([string isEqual:@"e"]||[string isEqual:@"E"])
    {
        temp = 14;
    }else if ([string isEqual:@"f"]||[string isEqual:@"F"])
    {
        temp = 15;
    }
    return temp;
}

//hexString 转换成 String
+ (NSString *)stringFromHexString:(NSString *)hexString {
    
    char *myBuffer = (char *)malloc((int)[hexString length] / 2 + 1);
    bzero(myBuffer, [hexString length] / 2 + 1);
    for (int i = 0; i < [hexString length] - 1; i += 2) {
        unsigned int anInt;
        NSString * hexCharStr = [hexString substringWithRange:NSMakeRange(i, 2)];
        NSScanner * scanner = [[NSScanner alloc] initWithString:hexCharStr];
        [scanner scanHexInt:&anInt];
        myBuffer[i / 2] = (char)anInt;
    }
    NSString *unicodeString = [NSString stringWithCString:myBuffer encoding:4];

    return unicodeString; 
    
}

调用方法

    NSString *headStr = @"https";
    NSString *playload = @"{\"fileName\":\"速度与激情.h264\"}";
    NSString *cmd = @"openStream";
    
    NSString *headHex = [self hexStringFromString:headStr];
    
    NSString *cmdHex = [self hexStringFromString:cmd];

    NSString *cmdLen = [self intToHexString:cmdHex.length/2 length:4];

    NSString *playloadHex = [self hexStringFromString:playload];
    
    NSString *playloadHexLen = [self intToHexString:playloadHex.length/2 length:8];

    NSString *allHexStr = [self stringWithFormat:@"%@%@%@%@%@",headHex,cmdLen,cmdHex,playloadHexLen,playloadHex];

     NSData *lastData = [self HexStringToData:allHexStr];
     NSLog(@"lastData=>%@",lastData);


最后打印出的Data,就是对应数据的16进制标准格式了。

其实还有一些可优化的地方,仔细的观察下每次的Data组装,然后你就会发现NSData本来就已经是16进制的,而大小端的转换其实还有跟简单的转换比方说是这种:

#include 

#define ntohs(x) __DARWIN_OSSwapInt16(x)
#define htons(x) __DARWIN_OSSwapInt16(x)

#define ntohl(x) __DARWIN_OSSwapInt32(x)
#define htonl(x) __DARWIN_OSSwapInt32(x)

这里写成这样完全是方便理解、

你可能感兴趣的:(iOS 关于大小端以及一些数据补位)