-1

I have a string variable in iOS and I would like to convert that to a character array and then to a hex bytes like 0xD6, 0xD6 etc.

It will be great if there is a library in Objective-C that I can use for this

vedhanish
  • 251
  • 3
  • 15
  • What encoding do you want the bytes? And have you looked at the documentation for `NSString`? There are plenty of methods available depending on what you are actually trying to get. Please [edit] your question with a clear example of what you have and what you want exactly. – rmaddy May 08 '17 at 21:27

2 Answers2

2

swift 4

 string to byte:

 let strChar = "A" 
 let data1 = [UInt8](self.strChar.utf8)
Deepak Tagadiya
  • 2,187
  • 15
  • 28
1

may be answer is here:

string to chars:

NSString *s = @"Some string";
const char *c = [s UTF8String];

chars to hex:

- (NSData *)dataFromHexString {
    const char *chars = [self UTF8String];
    int i = 0, len = self.length;

    NSMutableData *data = [NSMutableData dataWithCapacity:len / 2];
    char byteChars[3] = {'\0','\0','\0'};
    unsigned long wholeByte;

    while (i < len) {
        byteChars[0] = chars[i++];
        byteChars[1] = chars[i++];
        wholeByte = strtoul(byteChars, NULL, 16);
        [data appendBytes:&wholeByte length:1];
    }

    return data;
}

reference:NSString (hex) to bytes

Community
  • 1
  • 1
jimbo
  • 42
  • 1
  • 7