我想使用uint64变量将十六进制字符串(例如“ 43a2be2a42380”)转换为其十进制表示形式。我需要这样做,因为我要实现一个充当键盘的RFID读取器,并且按键必须是十进制数字。
我看到了其他答案(在arduino中将HEX字符串转换为Decimal)并使用strtoul实现了解决方案,但它仅适用于32位整数,并且strtoull不可用。
uint64_t res = 0;
String datatosend = String("43a2be2a42380");
char charBuf[datatosend.length() + 1];
datatosend.toCharArray(charBuf, datatosend.length() + 1) ;
res = strtoul(charBuf, NULL, 16);
如何使用Arduino获取大十六进制字符串/字节数组的十进制数?
您可以自己实现:
#include <stdio.h>
#include <stdint.h>
#include <ctype.h>
uint64_t getUInt64fromHex(char const *str)
{
uint64_t accumulator = 0;
for (size_t i = 0 ; isxdigit((unsigned char)str[i]) ; ++i)
{
char c = str[i];
accumulator *= 16;
if (isdigit(c)) /* '0' .. '9'*/
accumulator += c - '0';
else if (isupper(c)) /* 'A' .. 'F'*/
accumulator += c - 'A' + 10;
else /* 'a' .. 'f'*/
accumulator += c - 'a' + 10;
}
return accumulator;
}
int main(void)
{
printf("%llu\n", (long long unsigned)getUInt64fromHex("43a2be2a42380"));
return 0;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句