關(guān)于大小端
所謂大端數(shù)據(jù)就是數(shù)據(jù)的高字節(jié)保存在內(nèi)存的低地址中蝴簇,而數(shù)據(jù)的低字節(jié)保存在內(nèi)存的高地址中春感,這樣的存儲模式有點(diǎn)兒類似于把數(shù)據(jù)當(dāng)作字符串順序處理:地址由小向大增加恩尾,而數(shù)據(jù)從高位往低位放壶熏;這和我們的閱讀習(xí)慣一致但壮。
所謂的小端數(shù)據(jù)就是數(shù)據(jù)的高字節(jié)保存在內(nèi)存的高地址中陶夜,而數(shù)據(jù)的低字節(jié)保存在內(nèi)存的低地址中凛驮,這種存儲模式將地址的高低和數(shù)據(jù)位權(quán)有效地結(jié)合起來,高地址部分權(quán)值高条辟,低地址部分權(quán)值低黔夭。(簡單的來說就是反過來存放數(shù)據(jù)了)
關(guān)于補(bǔ)位
數(shù)據(jù)補(bǔ)位,是指在將數(shù)據(jù)按照8個(gè)字節(jié)一段進(jìn)行加密或解密得到一段8個(gè)字節(jié)的密文或者明文羽嫡,最后一段不足8個(gè)字節(jié)本姥,按照需求補(bǔ)足8個(gè)字節(jié)(通常補(bǔ)00或者FF,根據(jù)實(shí)際要求不同)進(jìn)行計(jì)算杭棵,之后按照順序?qū)⒂?jì)算所得的數(shù)據(jù)連在一起即可婚惫。
舉個(gè)栗子??!
假設(shè)要發(fā)送一段這樣的數(shù)據(jù)(16進(jìn)制數(shù)據(jù)):
| head | cmdlen(2字節(jié)) | cmd | playloadLen (4 字節(jié)) | playload
這段數(shù)據(jù)的內(nèi)容大概是包含了:我有一個(gè)頭部標(biāo)記魂爪,然后用頭部之后的兩個(gè)字節(jié)作為描述何種命令的長度先舷,然后接上的是何種命令,然后接上了附帶信息的長度(4個(gè)字節(jié))滓侍,然后是附帶信息蒋川。
需要發(fā)送的信息為:**| https(頭部) | len | openStream | len2 | {@"速度與激情.h264",@"fileName"} **
//
// CtpTools.m
// NetworkTest
//
// Created by 飛天江郎 on 10/11/2016.
// Copyright ? 2016 EzioChen. All rights reserved.
//
//普通字符串轉(zhuǎn)換為十六進(jìn)制的。
+ (NSString *)hexStringFromString:(NSString *)string{
NSData *myD = [string dataUsingEncoding:NSUTF8StringEncoding];
Byte *bytes = (Byte *)[myD bytes];
//下面是Byte 轉(zhuǎn)換為16進(jìn)制撩笆。
NSString *hexStr=@"";
for(int i=0;i<[myD length];i++)
{
NSString *newHexStr = [NSString stringWithFormat:@"%x",bytes[i]&0xff];///16進(jìn)制數(shù)
if([newHexStr length]==1)
hexStr = [NSString stringWithFormat:@"%@0%@",hexStr,newHexStr];
else
hexStr = [NSString stringWithFormat:@"%@%@",hexStr,newHexStr];
}
return hexStr;
}
//編寫一個(gè)NSData類型數(shù)據(jù)
+(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;
}
//補(bǔ)位的方法
+(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];
}
//把整型轉(zhuǎn)化為16進(jìn)制的捺球,然后在補(bǔ)位,轉(zhuǎn)換成大端數(shù)據(jù)
+(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進(jìn)制的字符串
+(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(十六進(jìn)制)轉(zhuǎn)化為為十進(jìn)制整型
+(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;
}
//大小端數(shù)據(jù)轉(zhuǎn)換(其實(shí)還有更簡便的方法夕冲,不過看起來這個(gè)方法是最直觀的)
+(NSData *)dataTransfromBigOrSmall:(NSData *)data{
NSString *tmpStr = [self dataChangeToString:data];
NSMutableArray *tmpArra = [NSMutableArray array];
for (int i = 0 ;i<data.length*2 ;i+=2) {
NSString *str = [tmpStr substringWithRange:NSMakeRange(i, 2)];
[tmpArra addObject:str];
}
NSArray *lastArray = [[tmpArra reverseObjectEnumerator] allObjects];
NSMutableString *lastStr = [NSMutableString string];
for (NSString *str in lastArray) {
[lastStr appendString:str];
}
NSData *lastData = [self HexStringToData:lastStr];
return lastData;
}
+(NSString*)dataChangeToString:(NSData*)data{
NSString * string = [NSString stringWithFormat:@"%@",data];
string = [string stringByReplacingOccurrencesOfString:@"<" withString:@""];
string = [string stringByReplacingOccurrencesOfString:@">" 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 轉(zhuǎn)換成 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;
}
調(diào)用方法
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氮兵,就是對應(yīng)數(shù)據(jù)的16進(jìn)制標(biāo)準(zhǔn)格式了。
其實(shí)還有一些可優(yōu)化的地方耘擂,仔細(xì)的觀察下每次的Data組裝胆剧,然后你就會發(fā)現(xiàn)NSData本來就已經(jīng)是16進(jìn)制的絮姆,而大小端的轉(zhuǎn)換其實(shí)還有跟簡單的轉(zhuǎn)換比方說是這種:
#include <sys/cdefs.h>
#define ntohs(x) __DARWIN_OSSwapInt16(x)
#define htons(x) __DARWIN_OSSwapInt16(x)
#define ntohl(x) __DARWIN_OSSwapInt32(x)
#define htonl(x) __DARWIN_OSSwapInt32(x)
這里寫成這樣完全是方便理解醉冤、