2

I got the string representation of an NSData via NSLog and I would like to construct the original NSData again for testing purposes, what's the best way to go about doing this?

e.g.

<fe010200 00000000 00011012>
Boon
  • 40,656
  • 60
  • 209
  • 315
  • See See http://stackoverflow.com/questions/7805340/retrieve-nsdata-to-hex-by-length – Rob Napier Mar 05 '13 at 21:10
  • Also see http://stackoverflow.com/questions/7520615/how-to-convert-an-nsdata-into-an-nsstring-hex-string – Rob Napier Mar 05 '13 at 21:11
  • Thanks, similar in concept but not exactly the same - I am interested in going from @"fe010200 00000000 00011012" to the actual NSData containing these byte data. – Boon Mar 05 '13 at 21:21
  • Yes but there may be different ways. Isn't this what stackoverflow is for? – Boon Mar 05 '13 at 21:46
  • So to be clear, you want to take a string where every 2 characters represents a byte, and convert it into the corresponding sequence of bytes? – Carl Veazey Mar 05 '13 at 21:48
  • Yes. Take what you get from NSData's description and turn it back into NSData. – Boon Mar 05 '13 at 21:52
  • @HotLicks:You said that its may be 10 minutes programming job but not yet got the concrete solution for this . – V-Xtreme Apr 12 '13 at 12:09
  • @V-Xtreme - Because no one wants to do someone else's homework. – Hot Licks Apr 12 '13 at 15:11
  • @HotLicks - This is a legit question with potentially many possible approach. It's totally appropriate and legit for SO. – Boon Apr 12 '13 at 19:13
  • @Boon - I didn't say it wasn't legit. It's just that it's a simple task, yet one that must be customized to the particular circumstances, and hence best done by the person who needs it. There certainly are "many possible approach", but the variations are not that interesting or instructive, so the educational potential is minimal. – Hot Licks Apr 12 '13 at 19:28
  • @HotLicks First you suggest me to write it myself, then you said it's a homework question. Do you say that to a legit question? What about this - you do it in 10 mins, post your solution. I will give you a bounty if your solution is good? I disagree the educational potential is minimal. Bounty started. – Boon Apr 12 '13 at 20:02
  • OK, we'll see if anyone bothers. It's not a really interesting problem, and a 50 bounty isn't big enticement, so don't expect a whole lot. In the interim you could attempt to write it yourself, and post questions here if you have problems. That would be a more productive/educational use of everyone's time. – Hot Licks Apr 12 '13 at 20:27
  • 1
    I already wrote my own the day I asked. I don't think it's an elegant solution, that's why I asked. Use your time to help other people if you don't find this one interesting, rather than lecturing people to do this themselves and labeling this a homework question. The solution to this is very useful for unit testing. So don't assume it's not educational just because it's isn't so for you. – Boon Apr 12 '13 at 21:07
  • Possible duplicate of http://stackoverflow.com/questions/7317860/converting-hex-nsstring-to-nsdata – fishinear Apr 15 '13 at 12:15
  • @fishinear Awesome, looks like a lot of people don't think this is a waste of time. Please post as an answer and I will award the bounty to you. – Boon Apr 15 '13 at 18:41
  • Why don't you want to do it the right way - save the data itself – art-divin Apr 17 '13 at 13:35
  • @Boon I don't need a bounty for 15 seconds of work. – fishinear Apr 17 '13 at 17:44
  • @fishinear: First post some answer. He will not give you bounty in if you don't want . We are eager to watch that 15 sec. of work. – V-Xtreme Apr 22 '13 at 06:28
  • @Boon The 15 seconds of work I was referring to, was finding the duplicate question. It is customary on StackOverflow that if there exists a prior question with existing answers, you don't go add answers on a new, identical, question. – fishinear Apr 22 '13 at 10:13
  • @fishinear You can just post a link. I feel the answer in that link is better than what we have here. Someone must be awarded for doing the hard work of searching for answers. – Boon Apr 22 '13 at 13:10

2 Answers2

0

I guess something like:

NSArray *wordStrings =
    [string componentsSeparatedByCharactersInSet:
        [[NSCharacterSet alphanumericCharacterSet] invertedSet]];

NSMutableData *collectedData = [NSMutableData dataWithCapacity:wordStrings.count * sizeof(unsigned)];

for(NSString *word in wordStrings)
{
    NSScanner *scanner = [NSScanner scannerWithString:word];
    unsigned newInt;
    [scanner scanHexInt:&newInt];
    [collectedData appendBytes:&newInt length:sizeof(unsigned)];
}

It creates a scanner for every word after using NSString to enforce spacing whereas it'd be more efficient to do everything in the scanner but it's just for debugging, right? This way you get the wordStrings midpoint to make sure your assumptions about breaking up the string are accurate.

Tommy
  • 99,986
  • 12
  • 185
  • 204
0

Here is a couple of lines that does a simple scan for numbers and append their values to a NSMutableData object

    NSString *dataIn = @"<fe010200 00000000 00011012>";

    const char *ptr = [dataIn cStringUsingEncoding:NSUTF8StringEncoding];

    NSMutableData *data = [NSMutableData data];

    while (*ptr) {
        unsigned char c1 = *ptr;
        ptr++;
        if (isalpha(c1))
            c1 = (10 + c1 - 'a')<<4;
        else if (isnumber(c1))
            c1 = (c1 - '0')<<4;
        else
            continue;
        if (!*ptr)
            break; // Shouldn't occure -- bad input
        unsigned char c2 = *ptr;
        ptr++;
        if (isalpha(c2))
            c2 = 10 + c2 - 'a';
        else if (isnumber(c2))
            c2 = c2 - '0';
        c1 = c1 | c2;
        [data appendBytes:&c1 length:1];
    }

    NSLog(@"%@", data);
epatel
  • 45,805
  • 17
  • 110
  • 144