Converting NSData
to NSString
does not stop at null bytes. A NSString
can contain arbitrary Unicode characters, including embedded "NULL" characters.
Example:
char bytes[] = { 'H', 'e', 'l', 'l', 'o', 0, 'W', 'o', 'r', 'l', 'd', 0 };
NSData *data = [NSData dataWithBytes:bytes length:sizeof(bytes)];
NSString *str = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
NSLog(@"%@", str);
Output:
Hello
So it looks as if the string contains only 5 characters and the conversion stopped at the first null byte. But that is only the NSLog
output, in fact nothing is lost:
for (int i = 0; i < [str length]; i++) {
unichar c = [str characterAtIndex:i];
NSLog(@"%2d, %3d, %c", i, c, c);
}
Output:
0, 72, H
1, 101, e
2, 108, l
3, 108, l
4, 111, o
5, 0,
6, 87, W
7, 111, o
8, 114, r
9, 108, l
10, 100, d
11, 0,
So the string contains all data and nothing is lost. Probably the string is truncated later, when you send it to the server.
REMARK: Converting the encrypted data to a string seems problematic to me, because the data is interpreted in some character encoding. I have chosen NSASCIIStringEncoding
in this example, but according to the documentation this encoding is only valid for ASCII values 0…127.
If you add more information on how and in which format the encrypted data is sent to the server, we might be able to make suggestions how so solve this better.