1

I am getting NSMutableArray as response from custom Smart Router

(<01>,
    <ca>,
    <04>,
    <3a>,
    <7b>,
    <22>,
    <70>,
    <61>,
    <73>,
    <73>,
    <22>,
    <3a>,
    <22>,
    <6b>,
    <61>,
    <6e>,
    <6f>,
    <6b>,
    <22>,
    <2c>,
    <22>,
    <73>,
    <73>,
    <69>,
    <64>,
    <22>,
    <3a>,
    <22>,
    <6f>,
    <70>,
    <65>,
    <6e>,
    <57>,
    <72>,
    <74>,
    <41>,
    <70>,
    <6c>,
    <31>,
    <32>,
    <38>,
    <22>,
    <2c>,
    <22>,
    <75>,
    <73>,
    <65>,
    <72>,
    <22>,
    <3a>,
    <22>,
    <6b>,
    <61>,
    <6e>,
    <6f>,
    <6b>,
    <22>,
    <7d>
)

I need to avoid the first 4 data then convert it to NSString of (ASCII) like below

{"mac":"kanok","pass":"kanok","passphrase":"root1234","ssid":"aplsh90","user":"kanok"}

I can convert it using this website

I know I have to use Convert hex to nsstring or Convert Hex to NSSting and stringByReplacingOccurrencesOfString

How Can I convert?

Shuvo Joseph
  • 894
  • 1
  • 12
  • 21
  • plz see this link http://stackoverflow.com/questions/6421282/how-to-convert-hex-to-nsstring-in-objective-c – balkaran singh Jun 02 '16 at 05:26
  • This is almost the same question of: http://stackoverflow.com/questions/37568631/convert-nsstring-of-hex-to-nsstring-of-text The real issue here is just about removing the unwanted values from a `NSMutableArray`, and use `componentsJoinedByString:`. – Larme Jun 02 '16 at 11:58

2 Answers2

1

This following code works for me.

-(void)processSmartRouterAssociationResponseWithArray:(NSMutableArray*)responseHexDataArray
{
    @try
    {
        //NSLog(@"responseHexDataArray : %@",responseHexDataArray);
        /* Ignore first 4 byte */
        [responseHexDataArray removeObjectAtIndex: 0];
        [responseHexDataArray removeObjectAtIndex: 0];
        [responseHexDataArray removeObjectAtIndex: 0];
        [responseHexDataArray removeObjectAtIndex: 0];

        // Convert Array to Srting
        NSString * responceHexString = [[responseHexDataArray valueForKey:@"description"] componentsJoinedByString:@""];
        //NSLog(@"responceHexStringWithBracket : %@",responceHexString);

        // Remove '<' and '>' from Hex response
        responceHexString = [[responceHexString stringByReplacingOccurrencesOfString:@"<"
                                                              withString:@""]
                    mutableCopy];
        responceHexString = [[responceHexString stringByReplacingOccurrencesOfString:@">"
                                                    withString:@""]
                  mutableCopy];

        //NSLog(@"responceHexString After Removing Bracket : %@",responceHexString);

        NSMutableString * responseJsonString = [[NSMutableString alloc] init];
        int i = 0;
        while (i < [responceHexString length])
        {
            NSString * hexChar = [responceHexString substringWithRange: NSMakeRange(i, 2)];
            int value = 0;
            sscanf([hexChar cStringUsingEncoding:NSASCIIStringEncoding], "%x", &value);
            [responseJsonString appendFormat:@"%c", (char)value];
            i+=2;
        }
        //NSLog(@"responseJsonString : %@",responseJsonString);
        [self insertRouterInfoToDBWithJson:responseJsonString];

    }
    @catch(NSException* exception)
    {
        NSLog(@"processSmartRouterAssociationResponseWithArray exception : %@",exception);
    }

}

I used code from Convert Hex to NSString and Convert NSArray to NSString.

Community
  • 1
  • 1
Shuvo Joseph
  • 894
  • 1
  • 12
  • 21
0

I'm not sure what type the objects in your NSMutableArray are, but if we assume they are NSString, the following works (using conversion code from this post):

#import "Foundation/Foundation.h"

// Extend NSString to initialize from an array of NSString hex values
@interface NSString (HexConversion)

+ (NSString *) stringFromHexArray:(NSArray *)array;

@end

@implementation NSString (HexConversion)

// Assumes NSArray is filled with NSStrings representing hex values, could easily be changed
+ (NSString *) stringFromHexArray:(NSArray *)array {
    NSMutableString *mString = [[NSMutableString alloc] init];
    for(NSString *hexItem in array) {
        int value = 0;
        sscanf([hexItem cStringUsingEncoding:NSASCIIStringEncoding], "%x", &value);
        [mString appendFormat:@"%c", (char)value];
    }
    return mString;
}

@end

int main(int argc, const char * argv[]) {
    @autoreleasepool {

        // Your test hex array
        NSArray *testArray = @[@"01", @"ca", @"04", @"3a", @"7b", @"22", @"70", @"61", @"73", @"73", @"22", @"3a", @"22", @"6b", @"61", @"6e", @"6f", @"6b", @"22", @"2c", @"22", @"73", @"73", @"69", @"64", @"22", @"3a", @"22", @"6f", @"70", @"65", @"6e", @"57", @"72", @"74", @"41", @"70", @"6c", @"31", @"32", @"38", @"22", @"2c", @"22", @"75", @"73", @"65", @"72", @"22", @"3a", @"22", @"6b", @"61", @"6e", @"6f", @"6b", @"22", @"7d"];

        // Convert to string!
        NSString *string = [NSString stringFromHexArray:testArray];

        // Verify conversion, prints Ê:{"pass":"kanok","ssid":"openWrtApl128","user":"kanok"}
        NSLog(@"%@", string); 
    }
    return 0;
}
Community
  • 1
  • 1
Carter
  • 3,053
  • 1
  • 17
  • 22