1匹表、Byte轉(zhuǎn)NSData
Byte value[3]={0};
value[0]=0x1B;
value[1]=0x99;
value[2]=0x01;
NSData * data = [NSData dataWithBytes:&value length:sizeof(value)];
優(yōu)點:這種方法比較簡單孽江,沒有進行轉(zhuǎn)換讶坯,直接一個字節(jié)一個字節(jié)的拼裝好發(fā)送出去。
缺點:當發(fā)送數(shù)據(jù)比較長時會很麻煩岗屏,而且不易更改辆琅。
2、NSString轉(zhuǎn)NSData
- (NSData *)hexToBytes:(NSString *)str{
NSMutableData* data = [NSMutableData data];
int idx;
for (idx = 0; idx+2 <= str.length; idx+=2) {
NSRange range = NSMakeRange(idx, 2);
NSString* hexStr = [str substringWithRange:range]; NSScanner* scanner = [NSScanner scannerWithString:hexStr];
unsigned int intValue;
[scanner scanHexInt:&intValue];
[data appendBytes:&intValue length:1];
}
return data;
}
3这刷、將十進制轉(zhuǎn)化為十六進制
- (NSString *)ToHex:(int)tepid{
NSString *nLetterValue;
NSString *str =@"";
int tamping;
for (int i = 0; i<9; i++) {
ttmpig=tmpid%16;
tmpid=tmpid/16;
switch (ttmpig) {
case 10:
nLetterValue =@"A";
break;
case 11:
nLetterValue =@"B";
break;
case 12:
nLetterValue =@"C";
break;
case 13:
nLetterValue =@"D";
break;
case 14:
nLetterValue =@"E";
break;
case 15:
nLetterValue =@"F";
break;
default:
nLetterValue = [NSString stringWithFormat:@"%u",ttmpig]; }
str = [nLetterValue stringByAppendingString:str];
if (tmpid == 0) {
break;
}
}
//不夠一個字節(jié)湊0
if(str.length == 1){
return [NSString stringWithFormat:@"0%@",str];
}else{
return str;
}
}
4婉烟、10進制轉(zhuǎn)2進制
- (NSString *)toBinarySystemWithDecimalSystem:(int)num length:(int)length{
int remainder = 0; //余數(shù)
int divisor = 0; //除數(shù)
NSString * prepare = @"";
while (true){
remainder = num%2;
divisor = num/2;
num = divisor;
prepare = [prepare stringByAppendingFormat:@"%d",remainder];
if (divisor == 0) {
break;
}
}
//倒序輸出
NSString * result = @"";
for (int i = length -1; i >= 0; i --){
if (i <= prepare.length - 1) {
result = [result stringByAppendingFormat:@"%@", [prepare substringWithRange:NSMakeRange(i , 1)]];
}else{
result = [result stringByAppendingString:@"0"];
}
}
return result;
}
5、2進制轉(zhuǎn)10進制
- (NSString *)toDecimalWithBinary:(NSString *)binary{
int ll = 0 ;
int temp = 0 ;
for (int i = 0; i < binary.length; i ++){
temp = [[binary substringWithRange:NSMakeRange(i, 1)] intValue];
temp = temp * powf(2, binary.length - i - 1);
ll += temp;
}
NSString * result = [NSString stringWithFormat:@"%d",ll];
return result;
}
6暇屋、16進制和2進制互轉(zhuǎn)
- (NSString *)getBinaryByhex:(NSString *)hex binary:(NSString *)binary{
NSMutableDictionary *hexDic = [[NSMutableDictionary alloc] init];
hexDic = [[NSMutableDictionary alloc] initWithCapacity:16];
[hexDic setObject:@"0000" forKey:@"0"];
[hexDic setObject:@"0001" forKey:@"1"];
[hexDic setObject:@"0010" forKey:@"2"];
[hexDic setObject:@"0011" forKey:@"3"];
[hexDic setObject:@"0100" forKey:@"4"];
[hexDic setObject:@"0101" forKey:@"5"];
[hexDic setObject:@"0110" forKey:@"6"];
[hexDic setObject:@"0111" forKey:@"7"];
[hexDic setObject:@"1000" forKey:@"8"];
[hexDic setObject:@"1001" forKey:@"9"];
[hexDic setObject:@"1010" forKey:@"a"];
[hexDic setObject:@"1011" forKey:@"b"];
[hexDic setObject:@"1100" forKey:@"c"];
[hexDic setObject:@"1101" forKey:@"d"];
[hexDic setObject:@"1110" forKey:@"e"];
[hexDic setObject:@"1111" forKey:@"f"];
NSMutableString *binaryString=[[NSMutableString alloc] init];
if (hex.length) {
for (int i=0; i<[hex length]; i++) {
NSRange rage;
rage.length = 1;
rage.location = i;
NSString *key = [hex substringWithRange:rage]; [binaryString appendString:hexDic[key]];
}
}else{
for (int i=0; i<binary.length; i+=4) {
NSString *subStr = [binary substringWithRange:NSMakeRange(i, 4)];
int index = 0;
for (NSString *str in hexDic.allValues) {
index ++;
if ([subStr isEqualToString:str]) {
[binaryString appendString:hexDic.allKeys[index-1]]; break;
}
}
}
}
return binaryString;
}
7似袁、int轉(zhuǎn)NSData
- (NSData *) setId:(int)Id {
//用4個字節(jié)接收
Byte bytes[4];
bytes[0] = (Byte)(Id>>24);
bytes[1] = (Byte)(Id>>16);
bytes[2] = (Byte)(Id>>8);
bytes[3] = (Byte)(Id);
NSData *data = [NSData dataWithBytes:bytes length:4];
}
8、NSData轉(zhuǎn)int
接受到的數(shù)據(jù)0x00000a0122
//4字節(jié)表示的int
NSData *intData = [data subdataWithRange:NSMakeRange(2, 4)];
int value = CFSwapInt32BigToHost(*(int*)([intData bytes]));//655650
//2字節(jié)表示的int
NSData *intData = [data subdataWithRange:NSMakeRange(4, 2)];int value = CFSwapInt16BigToHost(*(int*)([intData bytes]));//290
//1字節(jié)表示的intchar *bs = (unsigned char *)[[data subdataWithRange:NSMakeRange(5, 1) ] bytes];int value = *bs;//34
9、NSString轉(zhuǎn)化為UNICODE String
(NSString*)fname = @“Test”;
char fnameStr[10];
memcpy(fnameStr, [fname cStringUsingEncoding:NSUnicodeStringEncoding], 2*([fname length]));
10昙衅、NSString轉(zhuǎn)化為char
(NSString*)fname = @“Test”;
char fnameStr[10];
fnameStr =[fname UTF8String];
11扬霜、char -> NSData
方法一:
char * postData = "TEST";
NSData *data = [NSData dataWithBytes:postData length:strlen(postData)];
方法二:
轉(zhuǎn)換為NSString: - (id)initWithUTF8String:(const char *)bytes
然后用NSString的 - (NSData *)dataUsingEncoding:(NSStringEncoding)encoding
12、NSData ->char
NSData returnData 而涉;
char* bu=[returnData bytes];
13著瓶、NSData->NSString
NSString* aStr;
aStr = [[NSString alloc] initWithData:aData encoding:NSASCIIStringEncoding];
14、NSString->NSData
NSData* aData;
aData = [aStr dataUsingEncoding: NSASCIIStringEncoding];
15啼县、NSData 與 Byte
NSString *testString = @"1234567890";
NSData *testData = [testString dataUsingEncoding: NSUTF8StringEncoding];
Byte *testByte = (Byte *)[testData bytes];
for(int i=0;i<[testData length];i++)
printf("testByte = %d\n",testByte[i]);
16材原、Byte數(shù)組-> 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 *adata = [[NSData alloc] initWithBytes:byte length:24];
17、Byte數(shù)組->16進制數(shù)
Byte *bytes = (Byte *)[aData bytes];
NSString *hexStr=@"";
for(int i=0;i<[encryData length];i++)
{
NSString *newHexStr = [NSString stringWithFormat:@"%x",bytes[i]&0xff];///16進制數(shù)
if([newHexStr length]==1)
hexStr = [NSString stringWithFormat:@"%@0%@",hexStr,newHexStr];
else
hexStr = [NSString stringWithFormat:@"%@%@",hexStr,newHexStr];
}
NSLog(@"bytes 的16進制數(shù)為:%@",hexStr);
18季眷、將16進制數(shù)據(jù)轉(zhuǎn)化成Byte 數(shù)組
NSString *hexString = @"3e435fab9c34891f"; //16進制字符串
int j=0;
Byte bytes[128]; ///3ds key的Byte 數(shù)組余蟹, 128位
for(int i=0;i<[hexString length];i++)
{
int int_ch; /// 兩位16進制數(shù)轉(zhuǎn)化后的10進制數(shù)
unichar hex_char1 = [hexString characterAtIndex:i]; ////兩位16進制數(shù)中的第一位(高位*16)
int int_ch1;
if(hex_char1 >= '0' && hex_char1 <='9')
int_ch1 = (hex_char1-48)*16; //// 0 的Ascll - 48
else if(hex_char1 >= 'A' && hex_char1 <='F')
int_ch1 = (hex_char1-55)*16; //// A 的Ascll - 65
else
int_ch1 = (hex_char1-87)*16; //// a 的Ascll - 97
i++;
unichar hex_char2 = [hexString characterAtIndex:i]; ///兩位16進制數(shù)中的第二位(低位)
int int_ch2;
if(hex_char2 >= '0' && hex_char2 <='9')
int_ch2 = (hex_char2-48); //// 0 的Ascll - 48
else if(hex_char1 >= 'A' && hex_char1 <='F')
int_ch2 = hex_char2-55; //// A 的Ascll - 65
else
int_ch2 = hex_char2-87; //// a 的Ascll - 97
int_ch = int_ch1+int_ch2;
NSLog(@"int_ch=%d",int_ch);
bytes[j] = int_ch; ///將轉(zhuǎn)化后的數(shù)放入Byte數(shù)組里
j++;
}
NSData *newData = [[NSData alloc] initWithBytes:bytes length:128];
NSLog(@"newData=%@",newData);
19、將一個8個字節(jié)的字符串轉(zhuǎn)化成NSData
- (NSData *)change:(NSString *)string Length:(int)length{
Byte byte[8];
memset(byte, 0, 8 * sizeof(Byte));
NSData *data = [string dataUsingEncoding:NSASCIIStringEncoding];
Byte *stringByte = (Byte *)[data bytes];
memcpy(byte + 0*sizeof(Byte), stringByte , length*sizeof(Byte));
return [NSData dataWithBytes:byte length:8];
}