首页 > 代码库 > ios十进制、十六进制字符串,byte,data等之间的转换

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

十进制->十六进制

 

Byte bytes[]={0xA6,0x27,0x0A};

NSString *strIdL  = [NSStringstringWithFormat:@"%@",[[NSStringalloc]initWithFormat:@"%02lx",(long)bytes[0]]];

六进制->十进制

NSString *rechargeInfo =  @"0xff055008";

 

NSString *cardId2 = [rechargeInfosubstringWithRange:NSMakeRange(2,2)];

cardId2 = [NSStringstringWithFormat:@"%ld",strtoul([cardId2UTF8String],0,16)];

 

 

NSString *str = @"0xff055008";
//先以16为参数告诉strtoul字符串参数表示16进制数字,然后使用0x%X转为数字类型
unsignedlongred = strtoul([str UTF8String],0,16);
//strtoul如果传入的字符开头是“0x”,那么第三个参数是0,也是会转为十六进制的,这样写也可以:
unsignedlongred = strtoul([@"0x6587"UTF8String],0,0);
NSLog(@"转换完的数字为:%lx",red);

#pragma mark --字符串处理方法

//字符串处理方法

+(NSString *)cutString:(NSString *)str{

    NSString *targetStr=nil;

    if (str.length>9) {

        targetStr=[str substringFromIndex:9];

        

    }

    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=0;i< inter; i++) {

            string = [NSString stringWithFormat:@"0%@",str];

            str = string;

        }

    }

    return string;

}


+(NSString *)stringFromHexString:(NSString *)hexString {//

    

    char *myBuffer = (char *)malloc((int)[hexStringlength] /2 +1);

    bzero(myBuffer, [hexString length] / 2 + 1);

    for (int i =0; i < [hexStringlength] - 1; i +=2) {

        unsigned int anInt;

        NSString * hexCharStr = [hexString substringWithRange:NSMakeRange(i, 2)];

        NSScanner * scanner = [[NSScanneralloc]initWithString:hexCharStr] ;

        [scanner scanHexInt:&anInt];

        myBuffer[i / 2] = (char)anInt;

        NSLog(@"myBuffer is %c",myBuffer[i /2] );

    }

    NSString *unicodeString = [NSStringstringWithCString:myBufferencoding:4];

    NSLog(@"———字符串=======%@",unicodeString);

    return unicodeString; 

}


//10进制转16进制

+(NSString *)ToHex:(longlongint)tmpid

{

    NSString *nLetterValue;

    NSString *str =@"";

    long longint ttmpig;

    for (int i =0; i<9; i++) {

        ttmpig=tmpid%16;

        tmpid=tmpid/16;

        switch (ttmpig)

        {

            case 10:

                nLetterValue =http://www.mamicode.com/@"A";break;

            case 11:

                nLetterValue =http://www.mamicode.com/@"B";break;

            case 12:

                nLetterValue =http://www.mamicode.com/@"C";break;

            case 13:

                nLetterValue =http://www.mamicode.com/@"D";break;

            case 14:

                nLetterValue =http://www.mamicode.com/@"E";break;

            case 15:

                nLetterValue =http://www.mamicode.com/@"F";break;

            default:nLetterValue=http://www.mamicode.com/[[NSStringalloc]initWithFormat:@"%lli",ttmpig];

                

        }

        str = [nLetterValue stringByAppendingString:str];

        if (tmpid == 0) {

            break;

        }

        

    }

    return str;

}


//将16进制的字符串转换成NSData

+ (NSMutableData *)convertHexStrToData:(NSString *)str {

    if (!str || [str length] == 0) {

        return nil;

    }

    

    NSMutableData *hexData = http://www.mamicode.com/[[NSMutableDataalloc]initWithCapacity:8];

    NSRange range;

    if ([str length] %2 ==0) {

        range = NSMakeRange(0,2);

    } else {

        range = NSMakeRange(0,1);

    }

    for (NSInteger i = range.location; i < [strlength]; i +=2) {

        unsigned int anInt;

        NSString *hexCharStr = [str substringWithRange:range];

        NSScanner *scanner = [[NSScanneralloc]initWithString:hexCharStr];

        

        [scanner scanHexInt:&anInt];

        NSData *entity = [[NSDataalloc]initWithBytes:&anIntlength:1];

        [hexData appendData:entity];

        

        range.location += range.length;

        range.length = 2;

    }

  

    return hexData;

}


+ (NSString *)convertDataToHexStr:(NSData *)data {

    if (!data || [data length] == 0) {

        return @"";

    }

    NSMutableString *string = [[NSMutableStringalloc]initWithCapacity:[datalength]];

    

    [data enumerateByteRangesUsingBlock:^(constvoid *bytes,NSRange byteRange,BOOL *stop) {

        unsigned char *dataBytes = (unsignedchar*)bytes;

        for (NSInteger i =0; i < byteRange.length; i++) {

            NSString *hexStr = [NSStringstringWithFormat:@"%x", (dataBytes[i]) &0xff];

            if ([hexStr length] == 2) {

                [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=0;i<[myDlength];i++)

        

    {

        NSString *newHexStr = [NSStringstringWithFormat:@"%x",bytes[i]&0xff];///16进制数

        

        if([newHexStr length]==1)

            

            hexStr = [NSString stringWithFormat:@"%@0%@",hexStr,newHexStr];

        

        else

            

            hexStr = [NSString stringWithFormat:@"%@%@",hexStr,newHexStr]; 

    } 

    return hexStr; 

}

 

+(NSString *) parseByteArray2HexString:(Byte[]) bytes

{

    NSMutableString *hexStr = [[NSMutableStringalloc]init];

    int i = 0;

    if(bytes)

    {

        while (bytes[i] != ‘\0‘)

        {

            NSString *hexByte = [NSStringstringWithFormat:@"%x",bytes[i] &0xff];///16进制数

            if([hexByte length]==1)

                [hexStr appendFormat:@"0%@", hexByte];

            else

                [hexStr appendFormat:@"%@", hexByte];

            

            i++;

        }

    }

    NSLog(@"bytes的16进制数为:%@",hexStr);

    return hexStr;

}

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