0

I am working on an iOS test application to work with a Bluetooth 4.0 LE module I have developed. The module acts as a wireless UART connection to other electronic devices. I need to send an array of binary bytes through the peripheral. My setup already uses NSdata and transmits that via Bluetooth to the module.

Say for example I have an NSString @"ABCDE" then the string is converted into NSdata and transmitted, it arrives in my module as "ABCDE" works great, no problems.

However for one of my main applications I need to convert the charecters of the string into a proprietary byte array using a look up table. Then this byte array needs to become the NSdata and be transmitted.

Here is an example:

Let say I have the string @"A" a look up function determines each "A" becomes 5 binary bytes: "0111111 1001000 100100 0111111 0000000" (All letters consist of 5 bytes of binary data.

Now I need to store that in the NSData object so that it is sent through Bluetooth. The problem I am running into is that if I use the string representation of that data I will end up with a data object that is incorrect. I have tried many things but nothing seems to be working. Any help would be greatly appreciated!

Tanner Ewing
  • 337
  • 4
  • 18
  • Are you asking how to conver NSString to NSData? – Prince Agrawal Jan 10 '14 at 19:17
  • No, I guess I am asking how to set the actual bytes of the NSdata object to a binary byte. That way the first byte is "0111111" instead of the binary or ascii representation of the characters in a string. – Tanner Ewing Jan 10 '14 at 19:22
  • I am still unable to understand you clearly. See my answer in [this question](http://stackoverflow.com/questions/20772161/set-data-for-each-bit-in-nsdata-ios/20775752#20775752). It might be helpful. Let me know – Prince Agrawal Jan 10 '14 at 19:29

1 Answers1

0

This is just an example but I think you can get the point, just set the values of the byte array properly and pass it to the NSData:

char t[16];
t[0] = ((UUID >> 8) & 0xff);
t[1] = (UUID & 0xff);
NSData *data = [[NSData alloc] initWithBytes:t length:16];
seriakillaz
  • 833
  • 12
  • 22