我正在尝试将 NSString 转换为 uint8_t。我遇到的问题是 NSString 和生成的 uint8_t 变量不匹配。这是一些示例代码:
NSLog(@"Key now: %@", key);
NSData* keyData = [key dataUsingEncoding:NSUTF8StringEncoding];
const uint8_t *plainBuffer = (const uint8_t*)[data bytes];
size_t plainBufferSize = strlen((char *) plainBuffer);
NSLog(@"Plain buffer: %s", plainBuffer);
NSData* testData = [[NSData alloc] initWithBytes: plainBuffer length:plainBufferSize];
NSString* testString = [[NSString alloc] initWithData: testData encoding: NSUTF8StringEncoding];
NSLog(@"Test string: %@", testString);
示例输出:
Key now: 9iIWBpf5R6yu5pJ93l218RsMdWBLidXt
Plain buffer: 9iIWBpf5R6yu5pJ93l218RsMdWBLidXtMdWBLidXt
Test string: 9iIWBpf5R6yu5pJ93l218RsMdWBLidXtMdWBLidXt
显然它的 NSData -> uint8_t 步骤出错了,但我不知道为什么!