uint8_t转换为NSString

时间:2013-08-29 19:10:38

标签: ios objective-c

我需要将uint8_t缓冲区中单个元素的内容转换为NSString,以便我可以将其显示在iPhone应用程序的标签上。我从TCP连接中读取了缓冲区OK的内容。我没有得到正确的编码,因此可以正确显示元素的值。例如,如果buffer [4]的内容为0xFD或253,那么如何最好地将其转移到标签? (标签是data1)还是有更简单的方法?感谢。

uint8_t buffer[64]; 
uint8_t tinybuffer[1];
int len;

// Read in contents from TCP connection, log dump, and display to label.
len = [inputStream read:buffer maxLength:sizeof(buffer)];

if (len > 0) {
// Log dump
for(int i = 0; i < len; i++) {
    NSLog(@"Returning byte %d  : %x", i, buffer[i]);
}
NSLog(@"Finished Reading");

// Get data to the screen.
tinybuffer[0] = buffer[4];
NSString *str1 = [[NSString alloc] initWithBytes:tinybuffer length:1 encoding:NSUTF8StringEncoding];
_data1.text = str1;

1 个答案:

答案 0 :(得分:3)

NSString *str1 = [NSString stringWithFormat:@"%d", tinybuffer[0]];

应该做你想做的事。