简体   繁体   中英

Converting NSData to NSString without knowing the encoding type

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:

在此处输入图片说明

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];

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM