作者:朱克锋
转载请注明出处:http://blog.csdn.net/linux_zkf
+ (NSString*)hexStringForData:(NSData*)data
{
if (data == nil) {
return nil;
}
NSMutableString* hexString = [NSMutableString string];
const unsigned char *p = [data bytes];
for (int i=0; i < [data length]; i++) {
[hexString appendFormat:@"%02x", *p++];
}
return hexString;
}
+ (NSString*)hexStringForChar:(unsigned char *)data len:(int)len
{
if (len == 0) {
return nil;
}
NSMutableString* hexString = [NSMutableString string];
const unsigned char *p = data;
for (int i=0; i < len; i++) {
[hexString appendFormat:@"%02x", *p++];
}
return hexString;
}
+ (NSData*)dataForHexString:(NSString*)hexString
{
if (hexString == nil) {
return nil;
}
const char* ch = [[hexString lowercaseString] cStringUsingEncoding:NSUTF8StringEncoding];
NSMutableData* data = [NSMutableData data];
while (*ch) {
if (*ch == ' ') {
continue;
}
char byte = 0;
if ('0' <= *ch && *ch <= '9') {
byte = *ch - '0';
}
else if ('a' <= *ch && *ch <= 'f') {
byte = *ch - 'a' + 10;
}
else if ('A' <= *ch && *ch <= 'F') {
byte = *ch - 'A' + 10;
}
ch++;
byte = byte << 4;
if (*ch) {
if ('0' <= *ch && *ch <= '9') {
byte += *ch - '0';
} else if ('a' <= *ch && *ch <= 'f') {
byte += *ch - 'a' + 10;
}
else if('A' <= *ch && *ch <= 'F')
{
byte += *ch - 'A' + 10;
}
ch++;
}
[data appendBytes:&byte length:1];
}
return data;
}