我在使用CH32V303RCt6單片機(jī)中,通過(guò)NB模塊,連接到云平臺(tái),獲取到時(shí)間戳格式為“0000018CD2063DA0”樣式的,然后我進(jìn)行正常的轉(zhuǎn)化,發(fā)現(xiàn)定義的UINT64_T變量總是會(huì)出現(xiàn)溢出問(wèn)題,但是轉(zhuǎn)化的值1704330673568。并沒(méi)有超出64位大小,求解。代碼在C語(yǔ)言平臺(tái)運(yùn)行沒(méi)有問(wèn)題,應(yīng)該也是因?yàn)槭?4位的吧,我的測(cè)試歷程為
代碼傳上來(lái)就亂碼了,不知道為啥,看圖片吧
#include? #include? #include? uint64_t?power(int?base,?int?exponent) {?? ????uint64_t?result?=?1;?? ????for?(int?i?=?0;?i?<?exponent;?i++)?{?? ????????result?*=?base;?? ????}?? ????return?result;?? } uint64_t?Hex2Dec(char?*hex_arry)?? {?? ????int?value?=?0;?? ????uint64_t?dec_value?=?0;?? ?? ????for(int?i?=?strlen(hex_arry)-1;?i?>=?0;?i--)?? ????{?? ????????if(hex_arry[i]?>=?'0'?&&?hex_arry[i]?<=?'9')?? ????????{?? ????????????value?=?hex_arry[i]?-?'0';?? ????????}?? ????????else?if(hex_arry[i]?>=?'a'?&&?hex_arry[i]?<=?'f')?? ????????{?? ????????????value?=?(hex_arry[i]?-?'a'?+?10);?? ????????}?? ????????else?if(hex_arry[i]?>=?'A'?&&?hex_arry[i]?<=?'F')?? ????????{?? ????????????value?=?(hex_arry[i]?-?'A'?+?10);?? ????????}?? ????????else?? ????????{?? ????????????//?Handle?error?if?the?character?is?not?a?valid?hex?digit.?? ????????????printf("Invalid?hex?digit:?%c\n",?hex_arry[i]);?? ????????????continue;?? ????????}?? ?? ????????dec_value?+=?value?*?power(16,?15?-?i);?? ????????printf("dec_value[%d]?=?%ld\n",?i,?dec_value);?? ????}?? ?? ????return?dec_value;?? } int?main() { char?buf[]?=?"0000018CD2063DA0"; uint64_t?dec_value?=?0; dec_value?=?Hex2Dec(buf); ???printf("dec_value?=?%ld?\n",?dec_value); ??? ???return?0; }