0x转10进制 ios
2016-10-10 19:23:35 github_35041937 阅读数 397
-(NSString *) toHex:(long long int)num
{
    NSString * result = [NSString stringWithFormat:@"%llx",num];
    return [result uppercaseString];
}
      NSString *hex=[self toHex:[head.codeId intValue]];
      unsigned long red = strtoul([hex UTF8String],0,16);

      NSLog(@"转换完的数字为:%lx",red);

16进制字符串转NSdata

//每两个字节转一次
+ (NSData *)convertHexStrToData:(NSString *)str {
    if (!str || [str length] == 0) {
        return nil;
    }

    NSMutableData *hexData = [[NSMutableData alloc] initWithCapacity:8];
    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;
}
2016-07-05 10:49:44 wujianjun6932 阅读数 1498

    //10进制转16进制字符串

    NSString *hexString= [NSString stringWithFormat:@"%@",[[NSString alloc] initWithFormat:@"%1lx",20]];

    

2015-12-29 16:23:29 u013428812 阅读数 1676

最近自己也在做蓝牙开发,发现好多做蓝牙开发的都没有接触过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

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


2013-11-27 17:27:35 hong1595 阅读数 738

一步搞定,

[NSString stringWithFormat:@"%ld", strtoul([@"332e" UTF8String],0,16)];
2016-04-05 15:43:39 qq_26705543 阅读数 668

一、整形装换成两个字节

    

    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十进制转62进制

阅读数 1000

ios进制

阅读数 2

没有更多推荐了,返回首页