Search code examples
objective-cnsdata

Reading specified byte from NSData - output to label


I have a file which is loaded into a buffer. The file comes from an URL, and contains 31 bytes. So far so good.

How do I read a specific byte, let's say byte number 20, and then output a normal decimal ("converted" from binary) to a label?

For reference I've loaded the file into a NSData variable called 'data', an my label is called 'currentBattery'. :)


Solution

  • You can use [data bytes] and treat it like a character array and read 20. Or [data getBytes: &byte range: ...]

    ((char*)[data bytes])[20]
    

    Would be the simplest. I would check the length of the data to be sure first.