In an iOS app, I'm using a 3rd party SDK written in C.
As a result of an SDK method call I receive an unsigned char
array (see example below).
I need to convert this array to an Objective-C String (NSString) as to save it and later on convert it back to C to pass it as a parameter to another SDK method.
I've seen multiple ways to convert a C string to Objective-C.
NSString *example1 = [NSString stringWithFormat:@"%s", myArray]; // "8oFDO{c."rägÕªö"
NSString *example2 = [[NSString alloc] initWithBytes:myArray length:sizeof(myArray) encoding:NSASCIIStringEncoding]; // "8oFDO{c."rägÕªö"
...
But none of them seems to allows \0
(null-terminating character) in the middle (see [29] in the example below).
Example unsigned char array response:
{
[0] = '8'
[1] = '\b'
[2] = '\x01'
[3] = 'o'
[4] = '\x01'
[5] = '\x03'
[6] = 'F'
[7] = 'D'
[8] = 'O'
[9] = '\x02'
[10] = '\x10'
[11] = '\x0e'
[12] = '{'
[13] = '\x8d'
[14] = 'c'
[15] = '.'
[16] = '\x19'
[17] = '"'
[18] = 'r'
[19] = '\xe4'
[20] = 'g'
[21] = '\x18'
[22] = '\xd5'
[23] = '\xaa'
[24] = '\xf6'
[25] = '\x95'
[26] = '\x18'
[27] = '\x03'
[28] = '\x01'
[29] = '\0'
[30] = '\x04'
[31] = '\x01'
[32] = '\x05'
[33] = '\x05'
[34] = '\x01'
[35] = '\x05'
[36] = '\x06'
[37] = '\x01'
[38] = '\x01'
...
}
How can I convert from C to Objective-C and then back from Objective-C to C?