0

I need to parse an NSString to a byte array and am having some trouble doing it. I have a padded byte array in a method and convert that into a mutablestring, then I have a method that needs to place those numbers back into a byte array. In C# it would be as simple as:

do
{
    val = byte.Parse(str.Substring(i, 3));
    byteArr[j++] = val;
    i += 3;
}

Here is the code snippit Note** Ive been trying a lot of different things in the do loop so its a mess in there right now:

-(NSData*) StrToByteArray: (NSString*)str 
{
    NSLog(@"StrToByteArray. String: %@", str);
    if([str length]==0)
       NSLog(@"Invailid String");
    int val;
    Byte byteArr[[str length]/3];
    int i = 0;
    int j = 0;
    NSRange range; 
    do {
       range = NSMakeRange(i, 3);
       val = (int)[str substringFromIndex:i];
       NSLog(@"StrToByteArray. VAR: %i", val);
       byteArr[j++] = val;
       NSLog(@"byteArr: %i",byteArr[i]);
       i+=3;
    }while(i<str.length);
    NSData* wrappedByteArr = [NSData dataWithBytes:&byteArr length: sizeof(byteArr)];
    return wrappedByteArr;
}

Here is the loop that makes the padded string:

for(int i = 0; i<=len;i++)
{
    val = byteArr[i];
    NSLog(@"byteArr to string original: %i", val);
    if(val<(Byte)10)
    {
        [tempStr appendString:(@"00")];
        [tempStr appendString:[NSString stringWithFormat:@"%d",val]];
    }
    else if(val<(Byte)100)
    {
        [tempStr appendString:(@"0")];
        [tempStr appendString:[NSString stringWithFormat:@"%d",val]];
    }
    else {

        [tempStr appendString:[NSString stringWithFormat:@"%d",val]];
    }

}
NSLog(@"string: %@", tempStr);
return tempStr;
3
  • Sorry I posted an answer without realising what you're trying to do. Are you saying that your string actually contains numbers such as "123255000123" and you want to convert that into four bytes, 123, 255, 000 and 123? Could you show the string that you want to convert into data? Commented Jan 17, 2013 at 6:20
  • Sure, the string is 238095076083154150180036184206087006249074132222096 Commented Jan 17, 2013 at 6:23
  • So basically, you want to iterate the string 3 characters at a time, and for every iteration take the integer value of those three characters, and set that as the value for an index in your byte array? Commented Jan 17, 2013 at 6:27

1 Answer 1

1

Take 2

Now that I know what the data looks like and how you want to parse it, I would approach it like this:

- (NSData *) parseStringToData:(NSString *) str
{
    if ([str length] % 3 != 0)
    {
        // raise an exception, because the string's length should be a multiple of 3.
    }

    NSMutableData *result = [NSMutableData dataWithLength:[str length] / 3];
    unsigned char *buffer = [result mutableBytes];

    for (NSUInteger i = 0; i < [result length]; i++)
    {
        NSString *byteString = [str substringWithRange:NSMakeRange(i * 3, 3)];
        buffer[i] = [byteString intValue];
    }

    return result;
}

Edit:

Your padding method could be simplified as well by providing the correct format specifier that automatically pads integers.

for(int i = 0; i<=len;i++)
{
    val = byteArr[i];
    NSLog(@"byteArr to string original: %i", val);

    [tempStr appendFormat:@"%03d", val];
}
Sign up to request clarification or add additional context in comments.

4 Comments

Why don't you just make an NSMutableData of the appropriate size, then write to its mutableBytes?
@KurtRevis: Because I'm a C programmer with bad habits! Good suggestion, I'll edit my answer.
Wow that was fast, Awesome, let me try this out and I'll let you know the results. Thank you so much
Yes, this worked perfectly, Thank you. Im still kinda new to Objective C and some of the C++ and C# functions are complex to convert. Thank you again for your help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.