简体   繁体   English

将字符串转换为十六进制的 Arduino C 函数?

[英]Arduino C function to convert String to HEX?

I am looking for built-in C or C++ function that allows me to convert a float into an HEX string, so far I have used itoa , but it does not work with negative values since it works with unsigned for base 16, so I was wondering which one I could use instead that may handle the negative value.我正在寻找内置的 C 或 C++ 函数,它允许我将浮点数转换为十六进制字符串,到目前为止我已经使用itoa ,但它不适用于负值,因为它适用于基数为 16 的 unsigned,所以我是想知道我可以使用哪个可以处理负值。

Using itoa I loose my negative value as it can be seen below,使用 itoa 我失去了我的负值,如下所示,

Acceleration X: -9 | X angle: **-0.5156689167**
Acceleration Y: -69 | Y angle: **-3.9565520286**
Acceleration Z: 986 | Z angle: 80.4013519287
Value of ACC per axe (x,y,z) in HEX ->ffcdfe751f68
Data to be send x ->**ffcd**
Data to be send y ->**fe75**
Data to be send z ->1f68

What other function could I use with the same functionality?对于相同的功能,我还可以使用哪些其他功能?

Looking at the results you provided I would say they are correct.查看您提供的结果,我会说它们是正确的。 You got the binary complement values:你得到了二进制补码值:

ffcd = -51 in 16-Bit binary complement
fe75 = -395 in 16-Bit binary complement
1f68 = 8040 in 16-Bit binary complement

Devide it by 100 and you get your (rounded) floating point values.将其除以 100 即可得到(四舍五入的)浮点值。

atoi can handle negative values. atoi 可以处理负值。 It indicates the negative status by setting the most valued Bit of the binary representation to 1. You will not get a - sign, if you did expect one.它通过将二进制表示中最有价值的位设置为 1 来指示否定状态。如果您确实期望有 - 符号,则不会得到 - 符号。

You can compute the binary complement by yourself by converting the (16-Bit) HEX value to a decimal and substract 65536 from the result.您可以通过将(16 位)十六进制值转换为十进制值并从结果中减去 65536 来自己计算二进制补码。

eg例如

ffcd -dec-> 65485 -sub-> 65485 - 65536 = -51 -float-> -51 / 100.0 = - 0.51

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM