iOS -- 十进制、十六进制字符串,byte,data等之间的转换

时间:2021-07-20 10:14:04
十进制->十六进制
Byte bytes[]={0xA6,0x27,0x0A};
NSString *strIdL = [NSStringstringWithFormat:@"%@",[[NSStringalloc]initWithFormat:@"%02lx",(long)bytes[]]];
十六进制->十进制
NSString *rechargeInfo = @"0xff055008";
NSString *cardId2 = [rechargeInfosubstringWithRange:NSMakeRange(,)];
cardId2 = [NSStringstringWithFormat:@"%ld",strtoul([cardId2UTF8String],,)]; NSString
*str = @"0xff055008"; //先以16为参数告诉strtoul字符串参数表示16进制数字,然后使用0x%X转为数字类型 unsignedlongred
= strtoul([str UTF8String],,); //strtoul如果传入的字符开头是“0x”,那么第三个参数是0,也是会转为十六进制的,这样写也可以: unsignedlongred
= strtoul([@"0x6587"UTF8String],,); NSLog(@"转换完的数字为:%lx",red);
#pragma mark --字符串处理方法
//字符串处理方法
+(NSString *)cutString:(NSString *)str{
NSString *targetStr=nil;
if (str.length>) {
targetStr=[str substringFromIndex:]; }
return targetStr;
}
+(NSString *)transToStr:(NSData *)data{ NSString *str=[NSStringstringWithFormat:@"%@",data];
return str;
} //字符串补零操作
+(NSString *)addZero:(NSString *)str withLength:(int)length{
NSString *string = nil;
if (str.length==length) {
return str;
}
if (str.length<length) {
NSUInteger inter = length-str.length;
for (int i=;i< inter; i++) {
string = [NSString stringWithFormat:@"0%@",str];
str = string;
}
}
return string;
} +(NSString *)stringFromHexString:(NSString *)hexString {// char *myBuffer = (char *)malloc((int)[hexStringlength] / +);
bzero(myBuffer, [hexString length] / + );
for (int i =; i < [hexString length] - ; i += ) {
unsigned int anInt;
NSString * hexCharStr = [hexString substringWithRange:NSMakeRange(i, )];
NSScanner * scanner = [[NSScanneralloc] initWithString:hexCharStr] ;
[scanner scanHexInt:&anInt];
myBuffer[i / ] = (char)anInt;
NSLog(@"myBuffer is %c",myBuffer[i /] );
}
NSString *unicodeString = [NSStringstringWithCString:myBuffer encoding:];
NSLog(@"———字符串=======%@",unicodeString);
return unicodeString;
} //10进制转16进制
+(NSString *)ToHex:(longlong int)tmpid
{
NSString *nLetterValue;
NSString *str =@"";
long longint ttmpig;
for (int i =; i<; i++) {
ttmpig=tmpid%;
tmpid=tmpid/;
switch (ttmpig)
{
case :
nLetterValue =@"A";break;
case :
nLetterValue =@"B";break;
case :
nLetterValue =@"C";break;
case :
nLetterValue =@"D";break;
case :
nLetterValue =@"E";break;
case :
nLetterValue =@"F";break;
default:nLetterValue=[[NSStringalloc]initWithFormat:@"%lli",ttmpig]; }
str = [nLetterValue stringByAppendingString:str];
if (tmpid == ) {
break;
} }
return str;
} //将16进制的字符串转换成NSData
+ (NSMutableData *)convertHexStrToData:(NSString *)str {
if (!str || [str length] == ) {
return nil;
} NSMutableData *hexData = [[NSMutableDataalloc] initWithCapacity:];
NSRange range;
if ([str length] % == ) {
range = NSMakeRange(,);
} else {
range = NSMakeRange(,);
}
for (NSInteger i = range.location; i < [strlength]; i += ) {
unsigned int anInt;
NSString *hexCharStr = [str substringWithRange:range];
NSScanner *scanner = [[NSScanneralloc] initWithString:hexCharStr]; [scanner scanHexInt:&anInt];
NSData *entity = [[NSDataalloc] initWithBytes:&anIntlength:];
[hexData appendData:entity]; range.location += range.length;
range.length = ;
} return hexData;
} + (NSString *)convertDataToHexStr:(NSData *)data {
if (!data || [data length] == ) {
return @"";
}
NSMutableString *string = [[NSMutableStringalloc] initWithCapacity:[datalength]]; [data enumerateByteRangesUsingBlock:^(constvoid *bytes, NSRange byteRange,BOOL *stop) {
unsigned char *dataBytes = (unsignedchar*)bytes;
for (NSInteger i =; i < byteRange.length; i++) {
NSString *hexStr = [NSStringstringWithFormat:@"%x", (dataBytes[i]) &0xff];
if ([hexStr length] == ) {
[string appendString:hexStr];
} else {
[string appendFormat:@"0%@", hexStr];
}
}
}]; return string;
}
//十进制准换为十六进制字符串
+ (NSString *)hexStringFromString:(NSString *)string{
NSData *myD = [stringdataUsingEncoding:NSUTF8StringEncoding];
Byte *bytes = (Byte *)[myDbytes];
//下面是Byte转换为16进制。
NSString *hexStr=@"";
for(int i=;i<[myDlength];i++) {
NSString *newHexStr = [NSStringstringWithFormat:@"%x",bytes[i]&0xff];///16进制数 if([newHexStr length]==) hexStr = [NSString stringWithFormat:@"%@0%@",hexStr,newHexStr]; else hexStr = [NSString stringWithFormat:@"%@%@",hexStr,newHexStr];
}
return hexStr;
}
+(NSString *) parseByteArray2HexString:(Byte[]) bytes
{
NSMutableString *hexStr = [[NSMutableString alloc]init];
int i = ;
if(bytes)
{
while (bytes[i] != '\0')
{
NSString *hexByte = [NSString stringWithFormat:@"%x",bytes[i] & 0xff];///16进制数
if([hexByte length]==)
[hexStr appendFormat:@"0%@", hexByte];
else
[hexStr appendFormat:@"%@", hexByte]; i++;
}
}
NSLog(@"bytes 的16进制数为:%@",hexStr);
return hexStr;
}