转自
http://www.eoeit.cn/?post=40
我们做移动支付常常需要各种数据编码,希望分享给大家有所帮助,代码在如下:
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; }