Arduino - 如何将双精度转换为十六进制格式

Arduino - How to convert double to HEX format

我有一个 arudino 代码,我可以从中获取一些温度读数:

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

输出例如:26.23

我需要将其转换为 2623,然后转换为十六进制值,这样我就得到:0x0A3F

有线索吗?

我猜你的浮点值总是得到小数点后两位的数字。因此,您只需将从传感器读取的值乘以 100。

decimalValue = 100 * c1

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

您可以找到完整教程here

// 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;
}