Is interpreting sensor value safer with unsigned?

I am currently working on spi sensor that spits out 16-bit two's complement system value, since I have signed 16-bit integer data type (sint16) which are also represented in two's complement as well, would there be any issues just assigning the value from spi to the sint16 data type? The value that I have reads out works fine, but someone criticized this method saying it was not safe, saying that unsigned is better, and I should do some conversion to fit into unsigned variable. May I know have I missed something that has made this operation not safe? Just want to take this chance to learn if I'm wrong.
If we expect that a signed integer sensor_value should be non-negative,
to be safe, check it: if( sensor_value < 0 ) { /* got an unexpected value */ }
Just treating it as unsigned would only give an illusion of safety.
you need to refer to the documentation for the sensor. The two key things for what you have are its byte order (endian) and its signedness. If your peer is right or wrong, the documents will tell you. If its signed, you also need to know the storage approach, which it sounds like you do (2s comp).
Last edited on
I have read the application details and it is signed 16-bit number in two's complement format. In terms of endianness, least/most significant 8-bit are specified as well, so I only have to know endianness of my system to convert it properly. Thanks jonnin for the clarification.

Topic archived. No new replies allowed.