0

Disclaimer:

  • this previous question does not contain a valid answer.
  • this other instead does contain the technique I used to convert it but does not answer the question I have.

Question:

I am reading some NSData from a BLE characteristic which corresponds to a string, however I have no idea what encoding format it uses.

This is the view from the debugger on Xcode:

enter image description here

How can I convert it to a string? I'd like to extract this value: "<951c7c1d e0fbdcc3 7b8b0b97 5c522036>"

I tried the following with with no meanigful conversion. However XCode seems to do the job. Why?

NSData * valueAsData = characteristic.value.mutableCopy;

NSString* newStr = [[NSString alloc] initWithData:characteristic.value encoding:NSUTF8StringEncoding];
NSString* newStr2 = [[NSString alloc] initWithData:characteristic.value encoding:NSASCIIStringEncoding];
Community
  • 1
  • 1
mm24
  • 9,280
  • 12
  • 75
  • 170
  • Are you sure it's a NSString, and ONLY a string (could be a `struct` for instance) ? Are you trying to do some reverse engineering? If yes, and you are trying to determine the string encoding of your char, then you may have to try all the available encoding. Note, that there is some method that can convert a string "<951c7c1d e0fbdcc3 7b8b0b97 5c522036>" into the corresponding NSData so you don't have to redo the whole process to get to that line. Or are your trying to get a newStr = @""<951c7c1d e0fbdcc3 7b8b0b97 5c522036>" ? – Larme Jun 14 '16 at 14:58
  • xcode is showing byte values . you can do the same with NSData `bytes` – danh Jun 14 '16 at 15:16

1 Answers1

0

Get the descriptors associated with the characteristic. One of them should be CBUUIDCharacteristicFormatString. This will indicate the presentation format, which will indicate the data type of the characteristic value.

Jim
  • 72,985
  • 14
  • 101
  • 108