V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
iOS 开发实用技术导航
NSHipster 中文版
http://nshipster.cn/
cocos2d 开源 2D 游戏引擎
http://www.cocos2d-iphone.org/
CocoaPods
http://cocoapods.org/
Google Analytics for Mobile 统计解决方案
http://code.google.com/mobile/analytics/
WWDC
https://developer.apple.com/wwdc/
Design Guides and Resources
https://developer.apple.com/design/
Transcripts of WWDC sessions
http://asciiwwdc.com
Cocoa with Love
http://cocoawithlove.com/
Cocoa Dev Central
http://cocoadevcentral.com/
NSHipster
http://nshipster.com/
Style Guides
Google Objective-C Style Guide
NYTimes Objective-C Style Guide
Useful Tools and Services
Charles Web Debugging Proxy
Smore
aaronlam
V2EX  ›  iDev

NSString 里存着 NSData 的十六进制数据,怎么把它转回 NSData?

  •  
  •   aaronlam · 2015-12-13 17:50:43 +08:00 · 2630 次点击
    这是一个创建于 3070 天前的主题,其中的信息可能已经有所发展或是发生改变。
    最近在学 iOS 开发 正在做自己的一个小项目。但是遇到这么一个问题神奇的问题!

    NSString 里存着 NSData 的十六进制数据(类似于这样子的:<89504e47 0d0a1a0a 0......6258>),怎么把它转回 NSData ?

    搜了挺久貌似都没有具体的解决方案,所以只能拜托大家了!感谢!
    6 条回复    2015-12-16 00:26:06 +08:00
    loveuqian
        1
    loveuqian  
       2015-12-13 17:57:32 +08:00 via iPhone
    你是怎么把 data 存到 str 里面的
    dorentus
        2
    dorentus  
       2015-12-13 17:57:36 +08:00 via iPhone
    搜 hex to nsdata
    aaronlam
        3
    aaronlam  
    OP
       2015-12-13 18:07:36 +08:00
    @loveuqian T T 直接把 NSData 类型的放进了 stringWithFormat 后,悲剧就这么发生了!

    具体情况是这样的在我服务器端 Mysql 数据库里有个字段是用来存头像的(字段类型: MediumBlod ),然后我在 APP 端提供上传图片通过 JSON 存到 Mysql 数据库中,然后我想再次通过 JSON 从数据库拿回图片来显示的时候,发现通过 NSJSONSerialization 后的 NSData 变成了 NSString 类型(也就是说 NSString 里存着 NSData 的十六进制数据)
    allenforrest
        4
    allenforrest  
       2015-12-14 12:29:01 +08:00   ❤️ 1
    ```
    - (NSData *)dataFromHexString {
    const char *chars = [self UTF8String];
    NSUInteger i = 0, len = self.length;

    NSMutableData *data = [NSMutableData dataWithCapacity:len / 2];
    char byteChars[3] = {'\0','\0','\0'};
    unsigned long wholeByte;

    while (i < len) {
    byteChars[0] = chars[i++];
    byteChars[1] = chars[i++];
    wholeByte = strtoul(byteChars, NULL, 16);
    [data appendBytes:&wholeByte length:1];
    }

    return data;
    }
    ```

    拿走不谢。
    aaronlam
        5
    aaronlam  
    OP
       2015-12-16 00:25:44 +08:00
    +(NSData*)hexStringToByte:(NSString*)hexString
    {
    hexString=[[hexString uppercaseString] stringByReplacingOccurrencesOfString:@" " withString:@""];
    if ([hexString length]%2!=0)
    {
    return nil;
    }

    Byte tmpByt[1]={0};
    NSMutableData* bytes=[NSMutableData data];
    for(int i = 0; i < [hexString length]; i++)
    {
    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
    return nil;

    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
    return nil;

    tmpByt[0] = int_ch1+int_ch2; ///将转化后的数放入 Byte 数组里
    [bytes appendBytes:tmpByt length:1];
    }
    return bytes;
    }

    已解决!
    aaronlam
        6
    aaronlam  
    OP
       2015-12-16 00:26:06 +08:00
    @allenforrest 感谢!
    @dorentus 感谢!
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1130 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 22:48 · PVG 06:48 · LAX 15:48 · JFK 18:48
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.