+(NSString*)dataToString:(NSData*)data{
NSString*send1 = [datadescription];
NSMutableString*mStr = [[NSMutableStringalloc]initWithString:send1];
[mStrdeleteCharactersInRange:NSMakeRange(0,1)];
[mStrdeleteCharactersInRange:NSMakeRange(mStr.length-1,1)];
NSString*send = [mStrstringByReplacingOccurrencesOfString:@" "withString:@""];
returnsend;
}
+(NSString*)dataToHexString:(NSData*)data{
Byte*bytes = (Byte*)[databytes];
//下面是Byte转换为16进制。
NSString*hexStr=@"";
for(inti=0;i<[datalength];i++)
{
NSString*newHexStr = [NSStringstringWithFormat:@"%x",bytes[i]&0xff];///16进制数
if([newHexStrlength]==1)
hexStr = [NSStringstringWithFormat:@"%@0%@",hexStr,newHexStr];
else
hexStr = [NSStringstringWithFormat:@"%@%@",hexStr,newHexStr];
}
returnhexStr;
}
+(NSData*)hexStringToData:(NSString*)str
{
NSString*strTmp = [strstringByReplacingOccurrencesOfString:@"0x"withString:@""];
NSIntegerlengthTmp =strTmp.length/2;
SignedBytebytes[lengthTmp];
for(inti=0; i
intj = i*2;
NSString*tmp = [strTmpsubstringWithRange:NSMakeRange(j,2)];
unsignedintanInt;
NSScanner*scanner = [[NSScanneralloc]initWithString:tmp];
[scannerscanHexInt:&anInt];
bytes[i] = anInt;
}
return[NSDatadataWithBytes:byteslength:lengthTmp];
}
网友评论