ios常见的数据转换

转自
[b][url]http://www.eoeit.cn/?post=40[/url][/b]

我们做移动支付常常需要各种数据编码,希望分享给大家有所帮助,代码在如下:

NSString 转换成NSData 对象

NSData* xmlData = [@"testdata" dataUsingEncoding:NSUTF8StringEncoding];

16进制字符串BCD转asiic码nsdata

- (NSData *) stringToHexData
{
int len = (int)[self length] / 2; // Target length
unsigned char *buf = malloc(len);
unsigned char *whole_byte = buf;
char byte_chars[3] = {'\0','\0','\0'};

int i;
for (i=0; i < [self length] / 2; i++) {
byte_chars[0] = [self characterAtIndex:i*2];
byte_chars[1] = [self characterAtIndex:i*2+1];
*whole_byte = strtol(byte_chars, NULL, 16);
whole_byte++;
}

NSData *data = [NSData dataWithBytes:buf length:len];
free( buf );
return data;
}s

NSData 转换成NSString对象

NSData * data; NSString *result = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

nsdata转换成十六进制NSString对象




- (NSString *) dataToHexString { NSUInteger len = [self length]; char * chars = (char *)[self bytes]; NSMutableString * hexString = [[NSMutableString alloc] init]; for(NSUInteger i = 0; i < len; i++ ) [hexString appendString:[NSString stringWithFormat:@"%0.2hhx", chars[i]]]; return hexString; }

NSData 转换成char*

NSData *data; char *test=[data bytes];

char* 转换成NSData对象

byte* tempData = malloc(sizeof(byte)*16); NSData *content=[NSData dataWithBytes:tempData length:16];

byte转为十六进制bcd码

+(NSString*) convertByteToString:(Byte) data {
NSString* s=@"";
char c1 = (int)(data >> 4) + 0x30;
if( c1 > 0x39){
c1 = (int)(data >> 4) - 10 + 'A';
}
s = [NSString stringWithFormat:@"%@%c", s, c1];

char c2 = (int)(data & 0x0f) + 0x30;
if( c2 > 0x39){
c2 = (int)(data & 0x0f) - 10 + 'A';
}
s = [NSString stringWithFormat:@"%@%c ", s, c2];
return s;

}

//10 进制转16进制

+ (NSString *)intToHex:(int)tmpid
{
NSString *nLetterValue;
NSString *str =@"";
int ttmpig;
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:@"%d",ttmpig];

}
str = [nLetterValue stringByAppendingString:str];
if (tmpid == 0) {
break;
}

}
return str;
}

byte*转为十六进制bcd码

+(NSString*) convertByteToString:(Byte*) data length:(int)length { unsigned char *buf = malloc(length); Split(buf, data, length); return [NSString stringWithFormat:@"%s", buf]; }

static void Split(unsigned char *dest, const unsigned char *src, unsigned short pair_len) { unsigned short i = 0; unsigned short j = 0; if(src == NULL || dest == NULL) { return; } for(i = 0;i < pair_len;i++) { j = i*2; dest[j] = (src[i]&0xF0)>>4; if((dest[j] >= 0) && (dest[j] <= 9)) { dest[j] += 0x30; } else { switch(dest[j]) { case 0x0A: dest[j] = 'A'; break; case 0x0B: dest[j] = 'B'; break; case 0x0C: dest[j] = 'C'; break; case 0x0D: dest[j] = 'D'; break; case 0x0E: dest[j] = 'E'; break; case 0x0F: dest[j] = 'F'; break; default: break; } } dest[j+1] = src[i]&0x0F; if((dest[j+1] >= 0) && (dest[j+1] <= 9)) { dest[j+1] += 0x30; } else { switch(dest[j+1]) { case 0x0A: dest[j+1] = 'A'; break; case 0x0B: dest[j+1] = 'B'; break; case 0x0C: dest[j+1] = 'C'; break; case 0x0D: dest[j+1] = 'D'; break; case 0x0E: dest[j+1] = 'E'; break; case 0x0F: dest[j+1] = 'F'; break; default: break; } } } } static int str2hex(char *str, unsigned char *hex, int wStrLen) { unsigned char bLowByte,bHighByte; unsigned char bHiBase,bLowBase; int i; for(i = 0; i < wStrLen; i += 2) { bHighByte = toupper(str[i]); if ((bHighByte >= 'A') && (bHighByte <= 'F')) bHiBase = '7'; else if ((bHighByte >= '0') && (bHighByte <= '9')) bHiBase = '0'; else return -1; bLowByte = toupper(str[i+1]); if ((bLowByte >= 'A') && (bLowByte <= 'F')) bLowBase = '7'; else if ((bLowByte >= '0') && (bLowByte <= '9')) bLowBase = '0'; else return -1; hex[(i/2)] = ((bHighByte - bHiBase) * 16 + (bLowByte - bLowBase)); } return 0; }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值