2

I have a decimal value (read from BLE device with GATT) 256 (the same app also displays this value as 0x00010000 and indicate type as 16BIT FLOAT). Here is the description of the data structure.

How should I generate the same value as NSData?

LA_
  • 19,823
  • 58
  • 172
  • 308
  • have you tried this http://stackoverflow.com/questions/4689689/how-do-i-convert-an-nsnumber-to-nsdata – Teja Nandamuri Aug 20 '15 at 12:59
  • Update your question with some relevant code so we can see what you have. – rmaddy Aug 20 '15 at 14:36
  • @rmaddy, here is another question with the code - http://stackoverflow.com/questions/32121617/how-to-properly-reply-on-ble-didreceivereadrequest-for-hid-hid-information (since Joze already replied on the original question). – LA_ Aug 20 '15 at 14:56
  • Don't post a new question. Delete that one and update this one. – rmaddy Aug 20 '15 at 14:57
  • @rmaddy, this question is answered (and answered fully). Now I recognized that I should find answer for another question. They are related to the same task, but they are different. So, I think both questions should exist. – LA_ Aug 20 '15 at 14:58
  • If this question is answered then mark it so. – rmaddy Aug 20 '15 at 14:59

1 Answers1

1

You can do it like this since in the end it is a float value.

Encoding:

NSMutableData * data = [NSMutableData dataWithCapacity:0];
[data appendBytes:&yourfloatvalue length:sizeof(float)];

Decoding:

NSData * data = ...; // loaded from bluetooth
float yourfloatvalue;
[data getBytes:&yourfloatvalue length:sizeof(float)];

If you don't want to use NSMutableData then you can do this:

NSData * data = [NSData dataWithBytes:&yourfloatvalue length:sizeof(float)];
Jose Luis
  • 3,307
  • 3
  • 36
  • 53