h3c*_*t0r 3 c string hex arduino
我想使用uint64变量将十六进制字符串(如"43a2be2a42380")转换为十进制表示形式.我需要这个,因为我正在实现一个充当键盘的RFID阅读器,按键必须是十进制数字.
我已经看到了其他答案(在 HEduino中将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);
Run Code Online (Sandbox Code Playgroud)
如何使用Arduino获取大十六进制字符串/字节数组的十进制数?
...解决方案使用,
strtoul但它只适用于32位整数,strtoull不可用.
做两次使用strtoul(),一次用于低四个字节,一次用于其余部分并添加两个结果,0x100000000LLU预先乘以后者.
你可以自己实现:
#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;
}
Run Code Online (Sandbox Code Playgroud)