16进制数转data ios - CSDN
精华内容
参与话题
  • + (NSData *) stringToHexData:(NSString *)hexStr {  int len = [hexStr length] / 2; // Target length  unsigned char *buf = malloc(len);  unsigned char *whole_byte = buf;... char byte_c
    + (NSData *) stringToHexData:(NSString *)hexStr
    {
        int len = [hexStr length] / 2;    // Target length
        unsigned char *buf = malloc(len);
        unsigned char *whole_byte = buf;
        char byte_chars[3] = {'\0','\0','\0'};
        
        int i;
        for (i=0; i < [hexStr length] / 2; i++) {
            byte_chars[0] = [hexStr characterAtIndex:i*2];
            byte_chars[1] = [hexStr characterAtIndex:i*2+1];
            *whole_byte = strtol(byte_chars, NULL, 16);
            whole_byte++;
        }
        
        NSData *data = [NSData dataWithBytes:buf length:len];
        free( buf );
        return data;
    }


    //data转换为十六进制的string
    + (NSString *)hexStringFromData:(NSData *)myD{
        
        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];
        }
        NSLog(@"hex = %@",hexStr);
        
        return hexStr;
    }
    展开全文
  • iOS 二进制数组16进制字符串

    千次阅读 2015-08-28 16:02:37
    iOS 二进制数组16进制字符串 Byte bytes[28]; [data getBytes:bytes length:28]; NSMutableString *hexString = [[NSMutableString alloc] init]; for (int i = 0; i ; i++) {

    iOS 二进制数组转成16进制字符串

     Byte bytes[28];
     [data getBytes:bytes length:28];
    
     NSMutableString *hexString = [[NSMutableString alloc] init];
     for (int i = 0; i < 28; i++) {
          NSLog(@"%hhu",bytes[i]);
          [hexString appendString:[NSString stringWithFormat:@"%0.2hhx", bytes[i]]];
     }
    
     NSLog(@" %@  ", hexString);
    

    字符串转二进制

    int len = [self length] / 2;    // Target length  
    unsigned char *buf = malloc(len)  
    unsigned char *whole_byte = buf;  
    char byte_chars[3] = {'\0','\0','\0'};  
    
    int i;  
    for (i=0; i < [self length] / 2; i++) {  
        byte_chars[0] = [self characterAtIndex:i*2];  
        byte_chars[1] = [self characterAtIndex:i*2+1];  
        *whole_byte = strtol(byte_chars, NULL, 16);  
        whole_byte++;  
    }  
    
    NSData *data = [NSData dataWithBytes:buf length:len];  
    free( buf );  
    return data; 
    
    展开全文
  • iOS 16进制字符与NSData之间的转换

    千次阅读 2018-11-15 17:26:02
    #pragma mark - 十六进制转换工具 // 16进制转NSData - (NSData *)convertHexStrToData:(NSString *)str { if (!str || [str length] == 0) { return nil; } NSMutableData *hexData = [[NSMutableDa...
    #pragma mark - 十六进制转换工具
    // 16进制转NSData
    - (NSData *)convertHexStrToData:(NSString *)str
    {
        if (!str || [str length] == 0) {
            return nil;
        }
        
        NSMutableData *hexData = [[NSMutableData alloc] initWithCapacity:20];
        NSRange range;
        if ([str length] % 2 == 0) {
            range = NSMakeRange(0, 2);
        } else {
            range = NSMakeRange(0, 1);
        }
        for (NSInteger i = range.location; i < [str length]; i += 2) {
            unsigned int anInt;
            NSString *hexCharStr = [str substringWithRange:range];
            NSScanner *scanner = [[NSScanner alloc] initWithString:hexCharStr];
            
            [scanner scanHexInt:&anInt];
            NSData *entity = [[NSData alloc] initWithBytes:&anInt length:1];
            [hexData appendData:entity];
            
            range.location += range.length;
            range.length = 2;
        }
        return hexData;
    }
    // NSData转16进制 第一种
    - (NSString *)convertDataToHexStr:(NSData *)data
    {
        if (!data || [data length] == 0) {
            return @"";
        }
        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;
    }
    //普通字符串转换为十六进制的。
    - (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;
    }
    // 十六进制转换为普通字符串的。
    - (NSString *)stringFromHexString:(NSString *)hexString {
        
        char *myBuffer = (char *)malloc((int)[hexString length] / 2 + 1);
        bzero(myBuffer, [hexString length] / 2 + 1);
        for (int i = 0; i < [hexString length] - 1; i += 2) {
            unsigned int anInt;
            NSString * hexCharStr = [hexString substringWithRange:NSMakeRange(i, 2)];
            NSScanner * scanner = [[NSScanner alloc] initWithString:hexCharStr];
            [scanner scanHexInt:&anInt];
            myBuffer[i / 2] = (char)anInt;
        }
        NSString *unicodeString = [NSString stringWithCString:myBuffer encoding:4];
        return unicodeString;
    }
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }

     

    展开全文
  • 16进制字符串转换成16进制数,返回NSData类型的数据 - (NSData *)transformSignString:(NSString *)hexString{  int j=0;  NSInteger desLength = hexString.length/2;  if (hexString...

    将16进制字符串转换成16进制数,返回NSData类型的数据


    - (NSData *)transformSignString:(NSString *)hexString{


        int j=0;

        NSInteger desLength = hexString.length/2;

        if (hexString.length%2 != 0) {

            

            return nil;

        }

        Byte bytes[desLength]; ///3ds keyByte 数组, 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_char2 >= 'A' && hex_char2 <='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:desLength];

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

        return newData;

    }

    展开全文
  • 进制->十六进制 Byte bytes[]={0xA6,0x27,0x0A}; NSString *strIdL = [NSString stringWithFormat:@"%@",[[NSString alloc] initWithFormat:@"%02lx",(long)bytes[0]]]; 十六进制->十进制 NSString *re...
  • iOS16数据转换成2进制字符串

    千次阅读 2015-10-25 20:29:00
    下面这个是将16进制转换成2进制数据 + (NSData *)hex2data:(NSString *)hex { NSMutableData *data = [NSMutableData dataWithCapacity:hex.length / 2]; unsigned char whole_byte; char byte_chars[3] = {'\0'...
  • iOS 16进制转中文

    2019-06-17 19:09:11
    2019独角兽企业重金招聘Python工程师标准>>> ...
  • OC有符号16进制整形10进制整形

    千次阅读 2017-12-12 17:15:26
    16进制有符号判断正负其实是将16进制转为2进制看...这样我们可以这样想,我们不管这个16进制数是有符号还是无符号,我们都按无符号计算出来得到当前16进制数的无符号正数A,然后算出这个16进制数的-0也就是0X80.....
  • iOS 16进制转成汉字

    2015-10-25 20:20:46
    无论什么样的开发语言都会遇到进制的转换,十进制转16进制,16进制转汉字,10进制汉字,,,在银行项目中尤其用到16进制的转换,因为后台发来的报文多是16进制的,,, + (NSData *)chineseToHex:(NSString*)chinnese { ...
  • 进制流对于我们程序员来说不陌生,但是看起来那是相当困难,近期学习了一些进制的转换,希望对大家有用/** * 将二进制数据转换成十六进制字符串 * * @param data进制数据 * * @return 十六进制字符串 */ + ...
  • swift 16进制数-[UInt8]-字符串-String-

    千次阅读 2019-05-24 11:22:30
    把一串16进制数-[UInt8],例如 01 10 CD 成字符串-String "0110CD",代码如下: func bytesToStr(bytes:[UInt8]) -> String { var hexStr = "" for index in 0 ..< bytes.count { var Str = bytes...
  • ios 10进制转16进制

    2016-10-11 18:45:47
    -(NSString *) toHex:(long long int)num { NSString * result = [NSString stringWithFormat:@"%llx",num]; return [result uppercaseString]; } NSString *hex=[self toHex:[head.codeId intValue]];
  • ios 16进制加密 AES 128 ECB PKCS7Padding
  • 16进制NSString与NSData互

    千次阅读 2016-03-09 17:29:03
    - (NSData*)dataFormHexString:(NSString*)hexString; - (NSString *)hexStringFromData:(NSData*)data;
  • ios文字转为16进制

    2020-07-08 14:45:01
    //先以16为参数告诉strtoul字符串参数表示16进制数字,然后使用0x%X转为数字类型 unsigned long red = strtoul([str UTF8String],0,16); //strtoul如果传入的字符开头是“0x”,那么第三个参数是0,也是会转为...
  • - (NSData *) stringToHexData { int len = [self length] / 2; // Target length unsigned char *buf = (unsigned char *)malloc(len); unsigned char *whole_byte = buf; char byte_chars[3] =...
  • iOS16进制转nsdata

    2016-09-27 11:50:25
    //data转16 +(NSString *)DataToHexStr:(NSData *)data {  if (!data || [data length] == 0) {  return @"";  }  NSMutableString *string = [[NSMutableString alloc] initWithCapacity:[d
  • //先以16为参数告诉strtoul字符串参数表示16进制数字,然后使用0x%X转为数字类型 unsigned long red = strtoul([str UTF8String],0,16); //strtoul如果传入的字符开头是“0x”,那么第三个参数是0,也是会转为十六...
  • //先以16为参数告诉strtoul字符串参数表示16进制数字,然后使用0x%X转为数字类型 unsigned long red = strtoul([str UTF8String],0,16); //strtoul如果传入的字符开头是“0x”,那么第三个参数是0,也是会转为十六...
1 2 3 4 5 ... 20
收藏数 10,303
精华内容 4,121
热门标签
关键字:

16进制数转data ios