16进制数转换成字符串 ios_ios 16进制串hex与ascii字符串相互转换 - CSDN
精华内容
参与话题
  • iOS十六进制字符串的相互转换

    万次阅读 2013-08-31 20:48:30
    转自:... NSString *dictString = [dict JSONFragment];//组合的。  用这个就行了。。。 dictString==={"content":"Sadgfdfg","phoneno":"","email":"1049055935@qq.com"}=== //

    转自:http://mobile.51cto.com/hot-404914.htm


    1. NSString *dictString = [dict JSONFragment];//组合成的。 

    用这个就行了。。。

    1. dictString==={"content":"Sadgfdfg","phoneno":"","email":"1049055935@qq.com"}=== 
    2.  
    3. // 十六进制转换为普通字符串的。 
    4. + (NSString *)stringFromHexString:(NSString *)hexString { // 
    5.  
    6. char *myBuffer = (char *)malloc((int)[hexString length] / 2 + 1); 
    7. bzero(myBuffer, [hexString length] / 2 + 1); 
    8. for (int i = 0; i < [hexString length] - 1; i += 2) { 
    9. unsigned int anInt; 
    10. NSString * hexCharStr = [hexString substringWithRange:NSMakeRange(i, 2)]; 
    11. NSScanner * scanner = [[[NSScanner alloc] initWithString:hexCharStr] autorelease]; 
    12. [scanner scanHexInt:&anInt]; 
    13. myBuffer[i / 2] = (char)anInt; 
    14. NSString *unicodeString = [NSString stringWithCString:myBuffer encoding:4]; 
    15. NSLog(@"------字符串=======%@",unicodeString); 
    16. return unicodeString; 
    17.  
    18.  
    19.  
    20. //普通字符串转换为十六进制的。 
    21.  
    22. + (NSString *)hexStringFromString:(NSString *)string{ 
    23. NSData *myD = [string dataUsingEncoding:NSUTF8StringEncoding]; 
    24. Byte *bytes = (Byte *)[myD bytes]; 
    25. //下面是Byte 转换为16进制。 
    26. NSString *hexStr=@""
    27. for(int i=0;i<[myD length];i++) 
    28.  
    29. NSString *newHexStr = [NSString stringWithFormat:@"%x",bytes[i]&0xff];///16进制数 
    30.  
    31. if([newHexStr length]==1) 
    32.  
    33. hexStr = [NSString stringWithFormat:@"%@0%@",hexStr,newHexStr]; 
    34.  
    35. else 
    36.  
    37. hexStr = [NSString stringWithFormat:@"%@%@",hexStr,newHexStr]; 
    38. return hexStr; 
    39. }

    展开全文
  • IOS中常用的字符串、十进制、二进制、十六进制之间互相转换及简单算术和,希望能对大家有帮助
    //普通字符串转换为十六进制的。
    + (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 *)convertHexStrToString:(NSString *)hexString{
        if (!hexString || [hexString length] == 0) {
            return nil;
        }
        NSLog(@"line : %d, func: %s ",__LINE__, __func__);
        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] init];
            [scanner scanHexInt:&anInt];
            myBuffer[i / 2] = (char)anInt;
        }
        NSString *unicodeString = [NSString stringWithCString:myBuffer encoding:4];
        NSLog(@"------字符串=======%@",unicodeString);
        return unicodeString;
    }
    
    // 十六进制转二进制
    +(NSString *)getBinaryByhex:(NSString *)hex
    {
        NSMutableDictionary  *hexDic = [[NSMutableDictionary alloc] init];
        hexDic = [[NSMutableDictionary alloc] initWithCapacity:16];    
        [hexDic setObject:@"0000" forKey:@"0"];    
        [hexDic setObject:@"0001" forKey:@"1"];
        [hexDic setObject:@"0010" forKey:@"2"];    
        [hexDic setObject:@"0011" forKey:@"3"];    
        [hexDic setObject:@"0100" forKey:@"4"];    
        [hexDic setObject:@"0101" forKey:@"5"];    
        [hexDic setObject:@"0110" forKey:@"6"];    
        [hexDic setObject:@"0111" forKey:@"7"];    
        [hexDic setObject:@"1000" forKey:@"8"];    
        [hexDic setObject:@"1001" forKey:@"9"];    
        [hexDic setObject:@"1010" forKey:@"A"];    
        [hexDic setObject:@"1011" forKey:@"B"];    
        [hexDic setObject:@"1100" forKey:@"C"];    
        [hexDic setObject:@"1101" forKey:@"D"];    
        [hexDic setObject:@"1110" forKey:@"E"];    
        [hexDic setObject:@"1111" forKey:@"F"];    
        NSMutableString *binaryString=[[NSMutableString alloc] init];    
        for (int i=0; i<[hex length]; i++) {
            NSRange rage;        
            rage.length = 1;        
            rage.location = i;        
            NSString *key = [hex substringWithRange:rage];            
            binaryString = [NSString stringWithFormat:@"%@%@",binaryString,[NSString stringWithFormat:@"%@",[hexDic objectForKey:key]]];       
        }   
        return binaryString;    
    }
    //十进制转十六进制
    + (NSString *)ToHex:(uint16_t)tmpid
    {
        NSString *nLetterValue;
        NSString *str =@"";
        uint16_t 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 = [NSString stringWithFormat:@"%u",ttmpig];                
            }
            str = [nLetterValue stringByAppendingString:str];
            if (tmpid == 0) {
                break;
            }      
        }
        return str;
    }
    
    //十六进制简单算术和
    + (NSString *)hexsumstring:(NSString *)string{
        NSString *hexstring =string;
        int sum = 0;
        // 将16进制字符串,每两位一组截取并转换为10进制
        for (int i=0; i<hexstring.length-1; i=i+2) {
            long charint = strtoul([[hexstring substringWithRange:NSMakeRange(i, 2)] UTF8String], 0, 16);
            sum = sum+charint;
        }
        if (sum>256) {
            sum = sum-256;
        }
        hexstring = [self ToHex:sum];
        return hexstring;
    }
    
    展开全文
  • // 普通字符串转换为十六进制字符串 - (NSString *)hexStringFromString:(NSString *)string { NSData *myD = [string dataUsingEncoding:NSUTF8StringEncoding]; Byte *bytes = (Byte *)[myD bytes]; NSString...
    // 普通字符串转换为十六进制的字符串
    - (NSString *)hexStringFromString:(NSString *)string {
        NSData *myD = [string dataUsingEncoding:NSUTF8StringEncoding];
        Byte *bytes = (Byte *)[myD bytes];
        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;
    }

    展开全文
  • NSString *dictString = [dict JSONFragment];//组合的。用这个就行了。。。dictString==={"content":"...// 十六进制转换为普通字符串的。+ (NSString *)stringFromHexString:(N
    NSString *dictString = [dict JSONFragment];//组合成的。
    用这个就行了。。。
    dictString==={"content":"Sadgfdfg","phoneno":"","email":"1049055935@qq.com"}===
     
    // 十六进制转换为普通字符串的。
    + (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] autorelease];
            [scanner scanHexInt:&anInt];
            myBuffer[i / 2] = (char)anInt;
        }
        NSString *unicodeString = [NSString stringWithCString:myBuffer encoding:4];
        NSLog(@"------字符串=======%@",unicodeString);
        return unicodeString;
        
        
    }

    //普通字符串转换为十六进制的。

    + (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;
    }

     

     

     

     

    展开全文
  • IOS 如何把16进制字符串转换成byte?求各位大神 最好能有代码
  • 进制流对于我们程序员来说不陌生,但是... * 将二进制数据转换成十六进制字符串 * * @param data 二进制数据 * * @return 十六进制字符串 */ + (NSString *)data2Hex:(NSData *)data { if (!data) { return ni
  • ios oc 10进制转16进制字符串

    千次阅读 2016-07-05 10:49:44
    //10进制转16进制字符串  NSString *hexString= [NSString stringWithFormat:@"%@",[[NSString alloc] initWithFormat:@"%1lx",20]];
  • 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'...
  • 服务器端传一个这样格式的字符串:aabbcc 到客户端用来代表RGB。 如:a3b8e5 即表示:RGB red:163 green:184 blue:229. 那么怎样从 @“a3b8e5”中得到上面的结果? 有一个非常有用的函数:strtoul  ...
  • 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++) {
  • 很方便的c++字符串转换16进制

    万次阅读 2017-05-01 00:28:35
    鉴于网上搜的很多方法都有些复杂,很多名为c++,实则只是套了个c++的壳子,写的还是c语言的内容,并没有体现出c++语言本身的一些特色,我自己常识写了个字符串十六进制的函数,以之备忘。 需要用到的是ios::...
  • “#ffffff”类型的十六进制字符串转换成数字 分段截取计算UIColor的RGB 直接返回颜色 传入字符串后分段截取调用字符串转换Int的方法- (UIColor *)getColor:(NSString *)hexColor { NSString *string = [hexColor ...
  • //先以16为参数告诉strtoul字符串参数表示16进制数字,然后使用0x%X转为数字类型 unsigned long red = strtoul([str UTF8String],0,16); //strtoul如果传入的字符开头是“0x”,那么第三个参数是0,也是会转为十六...
  • + (NSData *) stringToHexData:(NSString *)hexStr {  int len = [hexStr length] / 2; // Target length  unsigned char *buf = malloc(len);  unsigned char *whole_byte = buf;... char byte_c
  • 1,编写代码需要用到的是ios:base中的hex,其实通过学习,很多事情会变得非常的方便 2具体代码如下所示: string string_to_hex(const string&amp; str) //transfer string to hex-string { string result=“0x...
  • iOS数字字符串转换为二进制进制十六进制
  • 本程序实现的功能是将ASCII格式的16进制字符串转到ASCII字符串。例如,字符串“6a61636b”可转化为jack。 啥都不说了,先给程序 // // main.cpp // hex_2_asc // // Created by 刘鹏 on 14-3-18. // Copyright (c) ...
  • 我将图片文件用二进制读取出来存入string中,然后转16进制字符串,方法如下: unsigned char c; char buf[3]; std::string result = ""; std::ifstream fread(file,std::ios::binary); while(fread.read...
  • 其中将十六进制字符串转换成NSData的代码如下: - (NSData *)convertHexStrToData:(NSString *)str { if (!str || [str length] == 0) { return nil; }   NSMutableData *hexData = [...
  • 字符串十六进制颜色值转换为UIColor: /** 16进制颜色转换为UIColor @param hexColor 16进制字符串(可以以0x开头,可以以#开头,也可以就是6位的16进制) @param opacity 透明度 @return 16进制字符串对应...
1 2 3 4 5 ... 20
收藏数 10,273
精华内容 4,109
关键字:

16进制数转换成字符串 ios