data转16进制 ios_data 转16进制 byte 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;
    }
    展开全文
  • 进制->十六进制 Byte bytes[]={0xA6,0x27,0x0A}; NSString *strIdL = [NSString stringWithFormat:@"%@",[[NSString alloc] initWithFormat:@"%02lx",(long)bytes[0]]]; 十六进制->十进制 NSString *re...

    十进制->十六进制

    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 < [hexString length] - 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:myBuffer encoding:4];

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

        return unicodeString; 

    }


    //10进制转16进制

    +(NSString *)ToHex:(longlong int)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 =@"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=[[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 = [[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 = [[NSMutableString alloc]init];

        int i = 0;

        if(bytes)

        {

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

            {

                NSString *hexByte = [NSString stringWithFormat:@"%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 二进制数组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; 
    
    展开全文
  • #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.
    }

     

    展开全文
  • 原文网址:... /// 将十六进制的字符串转化为NSData - (NSData )transStrHexToData:(NSString )strHex { /// bytes索引 NSUInteger j = 0; NSInteger len = strHex.length;...

    原文网址:http://www.voidcn.com/blog/u012198553/article/p-4976772.html

    /// 将十六进制的字符串转化为NSData 
    - (NSData )transStrHexToData:(NSString )strHex 

    /// bytes索引 
    NSUInteger j = 0;

    NSInteger len = strHex.length;
    
    if (len % 2 == 1) {
        /// 若不能被2整除则说明16进制的字符串不满足图片图。特此说明,假如只是单纯的把十六进制转换为NSData就把这个if干掉即可,
        return nil;
    }
    
    /// 动态分配内存
    Byte *bytes = (Byte *)malloc((len / 2 + 1) * sizeof(Byte));
    
    /// 初始化内存 其中memset的作用是在一段内存块中填充某个给定的值,它是对较大的结构体或数组进行清零操作的一种最快方法
    memset(bytes, '\0', (len / 2 + 1) * sizeof(Byte));
    
    /// for循环里面其实就是把16进制的字符串转化为字节数组的过程
    for (NSUInteger i = 0; i < strHex.length; i += 2) {
    
        /// 一字节byte是8位(比特)bit 一位就代表一个0或者1(即二进制) 每8位(bit)组成一个字节(Byte) 所以每一次取2为字符组合成一个字节 其实就是2个16进制的字符其实就是8位(bit)即一个字节byte
        NSString *str = [strHex substringWithRange:NSMakeRange(i, 2)];
    
        /// 将16进制字符串转化为十进制
        unsigned long uint_ch = strtoul([str UTF8String], 0, 16);
    
        bytes[j] = uint_ch;
    
        /// 自增
        j ++;
    }
    /// 将字节数组转化为NSData
    NSData *data = [[NSData alloc] initWithBytes:bytes length:len / 2];
    
    /// 释放内存
    free(bytes);
    
    return data;

    }

    展开全文
  • 进制流对于我们程序员来说不陌生,但是看起来那是相当困难,近期学习了一些进制的转换,希望对大家有用/** * 将二进制数据转换成十六进制字符串 * * @param data进制数据 * * @return 十六进制字符串 */ + ...
  • @implementation NSString (StringToHexData)    //  // Decodes an NSString containing hex encoded bytes into an NSData object  //  - (NSData *) stringToHexData  ...
  • 16进制有符号判断正负其实是将16进制转为2进制看最高位的值,如果是1则代表是负数,如果是0则代表正数,负数在计算机中是原数的补码,正数的补码就是原码,所以我们判断最高位是否是1,如果是1则最高位保持不变其余...
  • 原文:http://blog.sina.com.cn/s/blog_68661bd80101njdo.html //获取当前系统的时间戳+(long)getTimeSp{long time;NSDate *fromdate=[NSDate date];time=(long)[fromdate timeIntervalSince1970];return t...
  • 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
  • iOS des加密转码16进制字符串
  • //先以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] =...
  • ios 关于后台返回16进制数据的转化 一、如果是少量数据,可以转化为uicode编码,这时候已经不影响对数据进行处理 NSDictionary *responseDic = [NSJSONSerialization JSONObjectWithData:responseObject options:...
  • 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进行异或计算(^就代表异或操作),再高级语言中这种计算方式可以说是很陌生的,平常基本使用...
  • [objc] view ...//////////////AES加密方式: AES 128 ECB PKCS7Padding 16进制加密解密 //////////////   NSString *originalStr=@"AES";     //加密   NSString *encryStr = [AES128U
  • 将十六进制字符串转换成NSData 将NSData转换成十六进制的字符串
1 2 3 4 5 ... 20
收藏数 10,020
精华内容 4,008
关键字:

data转16进制 ios