美文网首页
进制转换

进制转换

作者: 丿ZLoveT | 来源:发表于2017-07-21 10:36 被阅读29次

    #pragma mark -从字符串中取字节数组

    -(NSData*)stringToByte:(NSString*)string{

    NSString *hexString=[[string uppercaseString] stringByReplacingOccurrencesOfString:@" " withString:@""];

    if ([hexString length]%2!=0) {

    return nil;

    }

    Byte tempbyt[1]={0};

    NSMutableData* bytes=[NSMutableData data];

    for(int i=0;i<[hexString length];i++){

    unichar hex_char1 = [hexString characterAtIndex:i]; ////两位16进制数中的第一位(高位*16)

    int int_ch1;

    if(hex_char1 >= '0' && hex_char1 <='9')

    int_ch1 = (hex_char1-48)*16;  //// 0 的Ascll - 48

    else if(hex_char1 >= 'A' && hex_char1 <='F')

    int_ch1 = (hex_char1-55)*16; //// A 的Ascll - 65

    else

    return nil;

    i++;

    unichar hex_char2 = [hexString characterAtIndex:i]; ///两位16进制数中的第二位(低位)

    int int_ch2;

    if(hex_char2 >= '0' && hex_char2 <='9')

    int_ch2 = (hex_char2-48); //// 0 的Ascll - 48

    else if(hex_char2 >= 'A' && hex_char2 <='F')

    int_ch2 = hex_char2-55; //// A 的Ascll - 65

    else

    return nil;

    tempbyt[0] = int_ch1+int_ch2;  ///将转化后的数放入Byte数组里

    [bytes appendBytes:tempbyt length:1];

    }

    return bytes;

    }


    #pragma mark - 十六进制转化为Byte数组

    - (NSData *)pppp:(NSString *)str{

    NSString *hexString = str; //16进制字符串

    int j=0;

    Byte bytes[1024];

    ///3ds key的Byte 数组, 128位

    for(int i=0;i<[hexString length];i++){

    int int_ch;  /// 两位16进制数转化后的10进制数

    unichar hex_char1 = [hexString characterAtIndex:i]; ////两位16进制数中的第一位(高位*16)

    int int_ch1;

    if(hex_char1 >= '0' && hex_char1 <='9')

    int_ch1 = (hex_char1-48)*16;  //// 0 的Ascll - 48

    else if(hex_char1 >= 'A' && hex_char1 <='F')

    int_ch1 = (hex_char1-55)*16; //// A 的Ascll - 65

    else

    int_ch1 = (hex_char1-87)*16; //// a 的Ascll - 97

    i++;

    unichar hex_char2 = [hexString characterAtIndex:i]; ///两位16进制数中的第二位(低位)

    int int_ch2;

    if(hex_char2 >= '0' && hex_char2 <='9')

    int_ch2 = (hex_char2-48); //// 0 的Ascll - 48

    else if(hex_char1 >= 'A' && hex_char1 <='F')

    int_ch2 = hex_char2-55; //// A 的Ascll - 65

    else

    int_ch2 = hex_char2-87; //// a 的Ascll - 97

    int_ch = int_ch1+int_ch2;

    NSLog(@"int_ch=%d",int_ch);

    bytes[j] = int_ch;  ///将转化后的数放入Byte数组里

    j++;

    }

    NSData *newData = [[NSData alloc] initWithBytes:bytes length:128];

    NSLog(@"newData=%@",newData);

    return newData;

    }


    #pragma mark - ASCll转十六进制

    - (Byte)ASCLL_HEX:(unsigned char)aHex{

    if((aHex>=0)&&(aHex<=9))

    aHex += 0x30;

    else if((aHex>=10)&&(aHex<=15))//A-F

    aHex += 0x37;

    else aHex = 0xff;

    return aHex;

    }


    #pragma mark - 十进制转化为十六进制

    - (NSString *)ToHex:(uint16_t)tmpid{

    NSString *nLetterValue;

    NSString *str =@"";

    uint16_t 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:@"%u",ttmpig];

    }str = [nLetterValue stringByAppendingString:str];

    if (tmpid == 0) {

    break;

    }

    }return str;

    }


    #pragma mark - 16进制转10进制

    + (NSNumber *) numberHexString:(NSString *)aHexString{

    if (nil == aHexString){

    return nil;

    }

    NSScanner * scanner = [NSScanner scannerWithString:aHexString];

    unsigned long long longlongValue;

    [scanner scanHexLongLong:&longlongValue];

    //将整数转换为NSNumber,存储到数组中,并返回.

    NSNumber * hexNumber = [NSNumber numberWithLongLong:longlongValue];

    return hexNumber;

    }


    #pragma mark - 字符串转十六进制

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

    NSUInteger len = [str length];

    unichar *chars = malloc(len * sizeof(unichar));

    [str getCharacters:chars];

    NSMutableString *hexString = [[NSMutableString alloc] init];

    for(NSUInteger i = 0; i < len; i++ ){

    [hexString appendString:[NSString stringWithFormat:@"%x", chars[i]]];

    }free(chars);

    return hexString;

    }


    #pragma mark - 字符串转十六进制

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

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

    return @"";

    }

    NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];

    NSMutableString *string = [[NSMutableString alloc] initWithCapacity:[data length]];

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

    unsigned char *dataBytes = (unsigned char*)bytes;

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

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

    if ([hexStr length] == 2) {

    [string appendString:hexStr];

    } else {

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

    }

    }

    }];return string;

    }


    #pragma mark 字符串转十六进制

    + (NSString *)hexStringFromString:(NSString *)string{

    NSData *myD = [string dataUsingEncoding:NSUTF8StringEncoding];

    Byte *bytes = (Byte *)[myD bytes];

    //下面是Byte 转换为16进制。

    NSString *hexStr=@"";

    for(int i=0;i<[myD length];i++){

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

    if([newHexStr length]==1)

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

    else

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

    }return hexStr;

    }

    相关文章

      网友评论

          本文标题:进制转换

          本文链接:https://www.haomeiwen.com/subject/twiskxtx.html