Converting NSString to NSData without changing data

旧街凉风 提交于 2019-12-12 02:45:03

问题


I have an NSString that contains a series of hex values, for example:

6173736e 616d65a2 15165570 6f696e74 584e534f 626a6563

However, I need this exact same data to be in an NSData object. I have tried doing things such as:

mydata = [mystring dataUsingEncoding:NSASCIIStringEncoding]; //have tried all kind of encoding options

Regardless of what I do though, mydata never contains the same values the NSString had, which is what I need.

Would greatly appreciate any help! Thank you.


回答1:


You seem to misunderstand what that method does. It doesn't parse the string for hexadecimal representations of numbers; it just creates a data object that represents the string with a certain byte encoding. So in your case, the data will contain bytes with the values 54 (the ASCII value for '6'), 49 (for '1'), 55 (for '7'), 51 (for '3'), 55 (for '7'), 51 (for '3'), 54 (for '6'), 101 (for 'e') and so on.

If you want to parse hexadecimal strings, you can use NSScanner to scan for hex values.

Here's the basic form of what you want:

NSScanner *scanner = [NSScanner scannerWithString:yourHexString];
NSMutableData *data = [NSMutableData data];
while (![scanner isAtEnd]) {
    unsigned value;
    if ([scanner scanHexInt:&value]) {
        [data appendBytes:&value length:sizeof(value)];
    } else {
        NSLog(@"Invalid value in scanned string");
    }
}

(Warning: Written in browser, haven't tested it, might cause a meltdown if you try to run a nuclear reactor with it, etc.)




回答2:


Taking the first character 6 as an example.

The hex value for 6 the character (0x360d0a according to this link, milage may vary depending on encoding) isn't the same as the hexvalue 0x6.

0x360d0a != 0x6

What you've got already appears to be data, though I'm not sure how you can write that data exactly again.

You may have to resort to something like:

-(NSData*) dataFromString:(NSString*) s
{
    NSMutableData *d = [[NSMutableData alloc] init] autorelease];
    for (int i = 0; i < [s length]; i++)
    { 
         char c = [s characterAtIndex:i];
         switch (c)
         {
             case '0': [d appendData:/*Add data to represent 0*/]; break;
             case '1': [d appendData:/*Is this append as simple as append:0x1*/]; break;
             ...
             case 'F': [d appendData:/*Add data to represent F*/]; break;
             //All 16 cases
         }
    }
    return d;
}



回答3:


Late to the party I know, but I recently had a similar problem, started with James Webster's answer and arrived at this:

-(NSData*) dataFromString:(NSString*) s
{
    NSMutableData *d = [[NSMutableData alloc] init];
    for (int i = 0; i < [s length]; i++)
    {
        char c = [s characterAtIndex:i];
        [d appendBytes:&c length:1];
    }
    return d;
}


来源:https://stackoverflow.com/questions/7800793/converting-nsstring-to-nsdata-without-changing-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!