我想將兩個8位數字組合成一個無符號整數,但無論我使用什麼類型的轉換,結果仍然是有符號數。該代碼使用CodeWarrior 10.1 for Freescale微處理器MC9S08LH64進行編譯。C中轉換有符號整數
我試過的東西沒有工作 - 移位並添加兩個8位數然後在每一步中將它們轉換爲無符號整型。 - 聯合/結構來結合兩個8位類型將它們和結果編號轉換爲無符號整型。 - 使用unsigned int類型的指針(下面的代碼)
unsigned int acquire_sensor_voltage_internal_adc()
{ //this is internal ADC
unsigned int result;
unsigned int* data_ptr;
char print_buffer [50];
int_convert cvt;
//internal adc collecting counts of input voltage
//______________________________________________
//writing to ADCSC1A initiate the conversion sequence
ADCSC1A= 0x09;
while(!ADCSC1A_COCOA){}
cvt.parts.p0 = ADCRHA;
cvt.parts.p1 = ADCRLA;
data_ptr = &cvt.int_number;
result = (unsigned int)*data_ptr;
sprintf(print_buffer,"here!!!>>>>>>>%d\r\n",result);
serial_sendString(print_buffer,strlen(print_buffer));
//_______________________________________________
return (unsigned int) result;
}
//definition of int_convert from.h file
typedef union{
unsigned int int_number;
struct{
unsigned char p0;
unsigned char p1;
}parts;
}int_convert;
怎麼樣(結果<<1)>> 1 – perilbrain 2012-08-16 19:02:46