Im trying to get my data retrieved from a socket into a nsmutablearray. however the examples and tutorials i found showed it going into a string first. which is fine i can parse it out from there. but i cant even get this string thing working.
case NSStreamEventHasBytesAvailable:
{
if(!rawData) {
rawData = [[NSMutableData data] retain];
}
uint8_t buf[1024];
unsigned int len = 0;
len = [(NSInputStream *)theStream read:buf maxLength:1024];
if(len) {
[rawData initWithBytes:buf length:len];
int bytesRead;
bytesRead += len;
[self messageReceived:rawData];
} else {
NSLog(@"no buffer!");
}
NSString *str = [[NSString alloc] initWithData:rawData
encoding:NSUTF8StringEncoding];
NSLog(@"data buffer: %@ |~|string buffer%@",rawData,str);
[str release];
break;
}
But as you will see from the output below the string never gets any of the data (well actually i think its an encoding problem, and so i think it just looks empty)
2011-09-27 13:14:06.356 Cameleon[30095:207] data buffer: <0f000102>
|~|string buffer2011-09-27 13:14:06.359 Cameleon[30095:207] data buffer: <02000400
000003> |~|string buffer2011-09-27 13:14:06.458 Cameleon[30095:207] data buffer: <05000500
00020300> |~|string buffer2011-09-27 13:14:06.659 Cameleon[30095:207] data buffer: <05000b00
0008080e 13163809 2711> |~|string buffer2011-09-27 13:14:06.663 Cameleon[30095:207] data buffer: <05000700
00040101 005a> |~|string buffer
i want the string buffer to mirror the values of the databuffer
or an array with each byte of the data buffer
ANSWER:
case NSStreamEventHasBytesAvailable:
{
if(!rawData) {
rawData = [[NSMutableData data] retain];
}
uint8_t buf[1024];
unsigned int len = 0;
len = [(NSInputStream *)theStream read:buf maxLength:1024];
if(len) {
[rawData initWithBytes:buf length:len];
} else {
NSLog(@"no buffer!");
}
const uint8_t *bytes = [rawData bytes];
NSMutableArray *mutableBuffer = [[NSMutableArray alloc] initWithCapacity:len];
for (int i =0; i < [rawData length]; i++) {
[mutableBuffer addObject:[NSString stringWithFormat:@"%02X", bytes[i]]];
}
[self gateKeeper:mutableBuffer];
[mutableBuffer release];
break;
Your code has several problems. First off, this is not the usual pattern for allocating an
NSDataobject:Although technically correct in terms of memory management, it’s non-idiomatic and results in an unnecessary pair of
autoreleaseandretainmessages getting sent. It should instead be this:Secondly, this code is useless:
You’re declaring a variable, failing to initialize it, adding
lento it (which is technically Undefined Behavior, but on x86 this will be harmless), and then doing nothing with it. You probably want to use a longer-lived variable declared outside of this block and initialize it properly.Finally, the actual cause of your problem is that the data you’re receiving is not UTF-8 text. It’s some kind of binary data with embedded NUL characters (the zero bytes). When these get converted to strings, the NULs signal termination of the string, so nothing after them gets printed.
Just keep the data you have as an
NSData, don’t bother trying to convert it to a string if it’s not actually textual data. What sort of data are you dealing with? Where is it coming from?