16进制转64进制 ios

2015-10-25 20:20:46 wang1018960145 阅读数 661
  • C++字符文件流操作

    掌握以下知识:1.C++对C的扩充2.类与对象3.继承与派生4.多态与虚 函数5.输入输出流6.标准模板库

    184人学习 沈寒
    免费试看

 无论什么样的开发语言都会遇到进制的转换,十进制转16进制,16进制转汉字,10进制转汉字,,,在银行项目中尤其用到16进制的转换,因为后台发来的报文多是16进制的,,,

+ (NSData *)chineseToHex:(NSString*)chinnese {
    NSStringEncoding encodingGB18030 = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);//自定义转换格式
    NSData *reponseData = [chinnese dataUsingEncoding:encodingGB18030];
    
    return reponseData;
}
返回的是data类型的

2018-11-15 17:26:02 qingshuiputeng 阅读数 1437
  • C++字符文件流操作

    掌握以下知识:1.C++对C的扩充2.类与对象3.继承与派生4.多态与虚 函数5.输入输出流6.标准模板库

    184人学习 沈寒
    免费试看
#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.
}

 

2016-05-09 22:48:49 l2i2j2 阅读数 669
  • C++字符文件流操作

    掌握以下知识:1.C++对C的扩充2.类与对象3.继承与派生4.多态与虚 函数5.输入输出流6.标准模板库

    184人学习 沈寒
    免费试看
- (void)RGBWithHexColor:(int)hexColor {
    int red = ((hexColor & 0xFF0000) >> 16);
    int green = ((hexColor & 0xFF00) >> 8);
    int blue = (hexColor & 0xFF);
    NSLog(@"red:%d  green:%d  blue:%d", red, green, blue);
}

2015-12-29 16:23:29 u013428812 阅读数 1719
  • C++字符文件流操作

    掌握以下知识:1.C++对C的扩充2.类与对象3.继承与派生4.多态与虚 函数5.输入输出流6.标准模板库

    184人学习 沈寒
    免费试看

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

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


2016-07-05 10:49:44 wujianjun6932 阅读数 1869
  • C++字符文件流操作

    掌握以下知识:1.C++对C的扩充2.类与对象3.继承与派生4.多态与虚 函数5.输入输出流6.标准模板库

    184人学习 沈寒
    免费试看

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

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

    

ios扩展16进制颜色

阅读数 8369