• IOS 如何把16进制字符串转换成byte?求各位大神 最好能有代码
  • 进制->十六进制 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;

    }




    展开全文
  • 最近自己也在做蓝牙开发,发现好多做蓝牙开发的都没有接触过C ,字节转换更是晕乎乎的,所以写下此篇博客,提供我转换的方法。 一、整形装换成两个字节  int value = 456;... unsigned char byte2 =

    最近自己也在做蓝牙开发,发现好多做蓝牙开发的都没有接触过C ,字节转换更是晕乎乎的,所以写下此篇博客,提供我转换的方法。

    一、整形装换成两个字节

        

        int value = 456;

        unsigned char  byte1 = (value & 0xff00)>>8; //8

        unsigned char  byte2 = (value & 0xff);//8

        

        NSLog(@"byte1= %x   byte2= %x ",byte1,byte2);

      

            byte1 = 1;  byte2= c8;

    二、两个字节转换成十进制整数

        char bytes[]={byte1,byte2};

        unsigned char  by1 = (bytes[0] & 0xff); //8

        unsigned char  by2 = (bytes[1] & 0xff);//8

        int   temp  = (by2|(by1<<8));

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

        

        temp = 456;

    三、整形装换成三个字节

        int value =134456;

        unsigned char  byte1 = (value & 0xff0000)>>16;//最高8

        unsigned char  byte2 = (value & 0xff00)>>8;//中间8

        unsigned char  byte3 = (value & 0xff);//8

        

        NSLog(@"byte1= %x   byte2= %x byte3= %x ",byte1,byte2,byte3);

        byte1= 2   byte2= d byte3= 38


    四、三个字节转换成十进制整数

        char bytes[]={byte1,byte2,byte3};

        unsigned char  by1 = (bytes[0] & 0xff); //8

        unsigned char  by2 = (bytes[1] & 0xff);//8

        unsigned char  by3 = (bytes[2] & 0xff);//8

        

        int   temp  = (by3|(by2<<8)|(by1<<16));

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

       temp = 134456

    如果还有更多的自己接需要转换以此类推


    展开全文
  • 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; 
    
    展开全文
  • 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;

    }

    展开全文
  • @implementation NSString (StringToHexData)    //  // Decodes an NSString containing hex encoded bytes into an NSData object  //  - (NSData *) stringToHexData  ...
    1. @implementation NSString (StringToHexData)  
    2.   
    3. //  
    4. // Decodes an NSString containing hex encoded bytes into an NSData object  
    5. //  
    6. - (NSData *) stringToHexData  
    7. {  
    8.     int len = [self length] / 2;    // Target length  
    9.     unsigned char *buf = malloc(len)  
    10.     unsigned char *whole_byte = buf;  
    11.     char byte_chars[3] = {'\0','\0','\0'};  
    12.   
    13.     int i;  
    14.     for (i=0; i < [self length] / 2; i++) {  
    15.         byte_chars[0] = [self characterAtIndex:i*2];  
    16.         byte_chars[1] = [self characterAtIndex:i*2+1];  
    17.         *whole_byte = strtol(byte_chars, NULL, 16);  
    18.         whole_byte++;  
    19.     }  
    20.   
    21.     NSData *data = [NSData dataWithBytes:buf length:len];  
    22.     free( buf );  
    23.     return data;  
    24. }  
    25. @end  
    26. @implementation NSData (DataToHexString)  
    27.   
    28. - (NSString *) dataToHexString  
    29. {  
    30.     NSUInteger          len = [self length];  
    31.     char *              chars = (char *)[self bytes];  
    32.     NSMutableString *   hexString = [[NSMutableString alloc] init];  
    33.   
    34.     for(NSUInteger i = 0; i < len; i++ )  
    35.         [hexString appendString:[NSString stringWithFormat:@"%0.2hhx", chars[i]]];  
    36.   
    37.     return hexString;  
    38. }  
    39. @end  
    展开全文
  • iOS10进制到16进制

    2015-03-26 17:03:47
    啥也不说了,直接上代码  int a = 16;  NSLog(@"a10=%o,a转16=%x",a,a); 打印出来是 a10=20,a转16=10
  • 16进制byte[]

    2008-09-07 15:52:16
    private byte[] toByteArray(String str) { String[] array=str.split(",0x"); array[0]=array[0].substring(2,array[0].length()); byte[] data=new byte[array.length]; for(int i=0;i&lt;...
  • #pragma mark - 十六进制转换工具 // 16进制转NSData - (NSData *)convertHexStrToData:(NSString *)str { if (!str || [str length] == 0) { return nil; } NSMutableData *hexData = [[NSMutableDa...
  • - (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] =...
  • + (NSData *) stringToHexData:(NSString *)hexStr {  int len = [hexStr length] / 2; // Target length  unsigned char *buf = malloc(len);  unsigned char *whole_byte = buf;... char byte_c
  • * @brief 将二进制数据转化为用字符表示的16进制数 * * @param data 二进制数据 * * @return 字符表示的16进制数 */ -(NSString *)HexStringWithData:(NSData *)data{ Byte *bytes = (Byte *)[data bytes]; ...
  • 原文网址:... /// 将十六进制的字符串转化为NSData - (NSData )transStrHexToData:(NSString )strHex { /// bytes索引 NSUInteger j = 0; NSInteger len = strHex.length;...
  • 首先,介绍一下字符串转换成... * @brief 字符串转换成16进制的字符  *  * @param string 要转换的字符串  *  * @return 返回得到的16进制字符串  */ + (NSString *)hexStringFromString:(NSString *)strin
  • Java版:private static byte charToByte(char c) { return (byte) "0123456789ABCDEF".indexOf(c); } public static String bytesToHexString(byte[] src){ StringBuilder stringBuilder = ...
  • //string 16进制 - (NSString *) dataToHexString {  NSUInteger len = [self length];  char * chars = (char *)[self bytes];  NSMutableString * hexString = [[NSMutableString allo
  • ios 字符串转16进制

    2019-06-14 04:00:09
    2019独角兽企业重金招聘Python工程师标准>>> ...
  • 进制流对于我们程序员来说不陌生,但是看起来那是相当困难,近期学习了一些进制的转换,希望对大家有用/** * 将二进制数据转换成十六进制字符串 * * @param data 二进制数据 * * @return 十六进制字符串 */ + ...
  • 一、整形装换成两个字节    int value = 456;  unsigned char byte1 = (value & 0xff00)>>8; //高8位 ... unsigned char byte2 = (value & 0xff);... NSLog(@"byte1= %x byte2= %x
1 2 3 4 5 ... 20
收藏数 4,552
精华内容 1,820