我在使用CH32V303RCt6单片机中,通过NB模块,连接到云平台,获取到时间戳格式为“0000018CD2063DA0”样式的,然后我进行正常的转化,发现定义的UINT64_T变量总是会出现溢出问题,但是转化的值1704330673568。并没有超出64位大小,求解。代码在C语言平台运行没有问题,应该也是因为是64位的吧,我的测试历程为
代码传上来就乱码了,不知道为啥,看图片吧
#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; }
热门产品 :
CH32X035: 32位USB+USB PD单片机