0

I need to send data over melodyManager & free from NonHexCharacters,

I already gone throught link How to convert an NSData into an NSString Hex string?

NSData from hex String?

In Both links i am not able to get NonHexFree charater data.

My input string is this,

0x0009 0xB0 0xFD 0xC2 0xA1 0x06 0x01%@040404 0x01 0xss1 0xhh 0xyy1

I need to convert this string into NSData which is free from nonhex characters i mean data should get only of hex string like from 0-9 symbols & A, B, C, D, E, F (alternatively a, b, c, d, e, f) and also remove space with 0x prefixes & output string should be 0009B0FDC2A10601040404111

then it should get converted to `NSData'

Community
  • 1
  • 1
  • This question is a little too abstract. What is melodyManager, what is NonHexFree? What did you do? Please show us code and depict where you got stuck. – Andreas Oetjen Feb 13 '17 at 07:58
  • 1
    So what is the result you would expect? Is it "000090B00FD0C20A100600104040400101001" – Andreas Oetjen Feb 13 '17 at 08:11
  • http://stackoverflow.com/a/7318062/1091539 – Mutawe Feb 13 '17 at 08:20
  • Yes nearly Clean a hex string by removing `spaces` and `0x` chars, `0x` because usually it is involving as a prefix thus output data should be get from this string only 0009B0FDC2A10601040404111. –  Feb 13 '17 at 08:22
  • @Mutawe i gone through your link too ... its not cleaning non hex characters. –  Feb 13 '17 at 08:24
  • I think you have to look into this http://stackoverflow.com/questions/1072744/checking-for-multiple-characters-in-nsstring – Mukesh Lokare Feb 13 '17 at 08:27
  • @Mukesh Can you add your code for scanning those nonhex characters? –  Feb 13 '17 at 08:53
  • Please edit your question, giving the wanted result from your input string, because it's clearly unclear what you want. Also, from where does come your input string, because your issue could be from getting that value instead. – Larme Feb 13 '17 at 09:12

1 Answers1

0

For cleaning nonHex characters check this,

NSString *input = @"0x0009 0xB0 0xFD 0xC2 0xA1 0x06 0x01%@040404 0x01 0xss1 0xhh 0xyy1";

I am considering that you don't required '0x' into your case.

NSString * output = [input stringByReplacingOccurrencesOfString:@"0x" withString:@""
                                                        options:NSCaseInsensitiveSearch range:NSMakeRange(0, input.length)];
NSString * hexChars = @"0123456789abcdefABCDEF";
NSCharacterSet *hexc = [NSCharacterSet characterSetWithCharactersInString:hexChars];
NSCharacterSet *invalidHexc = [hexc invertedSet];
NSString * allHex = [[output componentsSeparatedByCharactersInSet:invalidHexc] componentsJoinedByString:@""];

Then resultant hexstring pass to scan hexString & append into NSMUtableData then convert it into NSData,

//Check condition if required `allHex.lenght != nil`

NSMutableData *result = [[NSMutableData alloc] init];
int i = 0;
for (i = 0; i+2 <= allHex.length; i+=2) {
    NSRange range = NSMakeRange(i, 2);
    NSString* hexStr = [allHex substringWithRange:range];
    NSScanner* scanner = [NSScanner scannerWithString:hexStr];
    unsigned int intValue;
    [scanner scanHexInt:&intValue];
    unsigned char uc = (unsigned char) intValue;
    [result appendBytes:&uc length:1];
}
NSData * data = [NSData dataWithData:result];
Mukesh Lokare
  • 2,159
  • 26
  • 38