關(guān)于CH32V303單片機(jī)獲取到十六進(jìn)制字符串時(shí)間戳轉(zhuǎn)化為十進(jìn)制數(shù)字的問(wèn)題

我在使用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è)試歷程為1.png

1704334396206410.png

1704334396112184.png

代碼傳上來(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;
}

把value定義成 uint64_t 試試


您好,可將代碼放在txt文件中以附件形式發(fā)過(guò)來(lái),這邊測(cè)試一下。


回復(fù)2#,試過(guò)了,不行


icon_rar.giftiemr.zip

回復(fù)3#


回復(fù)2#,首先VALUE的值只是0-9和a-f之間的值,就不會(huì)超出這個(gè)int范圍,所以這塊不會(huì)溢出,只有這個(gè)返回值這塊會(huì)溢出


您好,關(guān)于你的程序問(wèn)題,主要打印導(dǎo)致的,按照下圖配置一下即可,附件為參考例程。

icon_rar.gifCH32V307 轉(zhuǎn)換.zip

image.png


感謝!


只有登錄才能回復(fù),可以選擇微信賬號(hào)登錄

国产91精品新入口,国产成人综合网在线播放,九热这里只有精品,本道在线观看,美女视频a美女视频,韩国美女激情视频,日本美女pvp视频