Arduino-如何将double转换为HEX格式



我有一个arudino代码,在那里我可以得到一些温度读数:

double c1 = device.readCelsius();
Serial.println(c1);

例如,输出为:26.23

我需要的是将其转换为2623,然后转换为HEX值,这样我就可以得到:0x0A3F

有线索吗?

我猜您的浮点值总是包含小数点后两位的数字。所以,你可以把从传感器上读取的值乘以100。

decimalValue = 100 * c1

然后您可以使用这个小代码将十进制值转换为HEX。感谢GeeksforGeeks

你可以在这里找到完整的教程

// C++ program to convert a decimal
// number to hexadecimal number

#include <iostream>
using namespace std;

// function to convert decimal to hexadecimal
void decToHexa(int n)
{
// char array to store hexadecimal number
char hexaDeciNum[100];

// counter for hexadecimal number array
int i = 0;
while (n != 0) {
// temporary variable to store remainder
int temp = 0;

// storing remainder in temp variable.
temp = n % 16;

// check if temp < 10
if (temp < 10) {
hexaDeciNum[i] = temp + 48;
i++;
}
else {
hexaDeciNum[i] = temp + 55;
i++;
}

n = n / 16;
}

// printing hexadecimal number array in reverse order
for (int j = i - 1; j >= 0; j--)
cout << hexaDeciNum[j];
}

// Driver program to test above function
int main()
{
int n = 2545;

decToHexa(n);

return 0;
}

最新更新