mike_ruv
mike_ruv

Reputation: 123

NSData too many and wrong order when sending bytes for wifi device

I am trying to send some data to a wireless device. The data I want to send is a byte array (i.e. Byte stuff[4]). I have done this with a serial cable and works fine. But for the wireless version, the NSData object is merging the bytes together. Let's say the first four bytes are 24, 130, 255, and 255. In hex, theses are 18, 82, FF, and FF. I want to send the bytes separately but when I check what is in the byte tha, the byte is 0xffff8218. I do not want to send something of the form 0x00000000, I would much rather send 0x00, just one byte at a time. How would I make NSData create an object that is only one byte rather than four bytes in reverse order? Thank you.

And some code: This is using the GCDAyncSocket.h and .m

Byte testing[15];
NSData *stuff;

basically load up a byte array then put it in NSData to be able to send

testing[0]=24;testing[1]=130;testing[2]=255;testing[3]=255;testing[4]=131;testing[5]=255;testing[6]=255;testing[7]=244;testing[8]=5;testing[9]=65;testing[10]=73;testing[11]=83;testing[12]=48;testing[13]=0;testing[14]=224;
stuff = [NSData dataWithBytes:&testing length:15];

when I put a breakpoint after that and before I send it, I see that the first byte of "stuff" is <0xffff8218>

Upvotes: 0

Views: 248

Answers (1)

Caleb
Caleb

Reputation: 125007

NSData manages a collection of bytes -- it doesn't know anything about the type of the data that those bytes represent, the endianness of the data, etc. You can get the bytes from an NSData object using the -bytes method, which gives you a const void *, and you can then send the bytes one at a time if you like.

Upvotes: 0

Related Questions