I have a NSData
with a hex value inside e.g. <EC>
.
Now I need to convert this value into an int
variable to get int x = -20
.
Ideas?
You can use getBytes
to extract that byte from the NSData
:
uint8_t byte;
[data getBytes:&byte length:1];
If you really need that in an int
, then grab it from that uint8_t
variable:
int x = byte;
As an aside, if you really need this 0xec
value interpreted as -20
rather than 236
, you can use int8_t
instead of uint8_t
, and the sign of this byte value will be preserved. Frequently, though, when dealing with binary data, we deal with unsigned bytes, rather than signed integers, but it just depends upon what this binary NSData
represents.