Estoy leyendo datos de una secuencia de TCP/IP y estoy recibiendo con éxito una matriz de bytes del servidor preexistente. Ahora estoy tratando de encontrar una forma de convertir esa matriz a NSString
. He encontrado varios ejemplos, pero estoy teniendo dificultades para obtener los resultados deseados.Cómo convertir una matriz de bytes a NSString
NSData *data=[[NSMutableData alloc] init];
uint8_t buffer[1024];
unsigned int len=0;
len=[(NSInputStream *)stream read:buffer maxLength:1024];
if(len>0){
[data appendBytes:&buffer length:len];
//BYTE ARRAY OBTAINED OK!!
///////////////////////////////////////////////////////
//METHOD #1 - Yields 'nil'
NSString *string = [[NSString alloc] initWithData:data
encoding:NSUTF8StringEncoding];
///////////////////////////////////////////////////////
//METHOD #2 - Log prints OK, but messageString says
//'invalid' in debugger, and get warnings all over the
//place. I know this is wrong, but it semi-works :)
size_t length=[data length];
unsigned char aBuffer[length];
[data getBytes:aBuffer length:length];
aBuffer[length - 1]=0;
NSString *messageString =aBuffer;
NSLog (@"%s",messageString);
///////////////////////////////////////////////////////
}else{
NSLog(@"No Buffer");
}
¡Por favor ayuda! Cualquier asistencia proporcionada es GRANMENTE apreciada.
por qué el downvotes? – Dutchie432