问题
I apologize beforehand if this is a beginner's question; I'm new to C programming.
I am working on an embedded design project that requires my ATmega128 reads in a 4-byte data from a sensor. The data contains two 14-bit measurements that I need to process. There is one part I'm not completely sure about because I haven't done a lot of C programming yet. My algorithm is as follows:
1) Receive 4 bytes of data from sensor (MS 2 bits and LS 2 bits are to be tossed) 2) Save each byte into 4 unsigned char variables 3) Mask out MS 2 bits from MS byte, same for LS 2 bits from LS byte 4) MS 2 bytes = first measurement, LS 2 bytes = second measurement
This part I can do so far. What I'm not sure about is how C converts binary to decimal. I need to turn my two 14-bit measurements into unsigned int to make it easier to perform calculations. Is there an instruction that automatically does this? Or will the compiler automatically turn it into an int if I use integer operations on it? Or maybe none of these, and I have to write my own function to convert it?
回答1:
All C data is represented as binary, so unsigned char values are really just integers. They are represented as binary, but you can just use them as numbers. So +, -, *, etc. work just as you would expect (just watch out for / because integer division and float division are different).
来源:https://stackoverflow.com/questions/22025641/converting-binary-to-int