简体   繁体   English

在Arduino上将整数/小数转换为十六进制?

[英]Convert integer/decimal to hex on an Arduino?

How can an integer or decimal variable be converted into a hex string? 如何将整数或十进制变量转换为十六进制字符串? I can do the opposite (convert hex to int) but I can't figure out the other way. 我可以做相反的事情(将十六进制转换为int),但我无法弄清楚其他方式。

This is for Serial.print() hex values in an array. 这适用于数组中的Serial.print()十六进制值。

Take a look at the Arduino String tutorial here . 这里查看Arduino字符串教程。 The code below was taken from that example. 下面的代码取自该示例。

// using an int and a base (hexadecimal):
stringOne =  String(45, HEX);   
// prints "2d", which is the hexadecimal version of decimal 45:
Serial.println(stringOne);  

There are plenty of other examples on that page, though I think for floating point numbers you'll have to roll your own. 该页面上还有很多其他示例,但我认为对于浮点数,您必须自己动手。

有一个简单的解决方案,只需使用:

Serial.print(yourVariable, HEX);

The Streaming library provides a built in way to do this: Streaming库提供了一种内置方式来执行此操作:

#include <Streaming.h>
...
Serial << "45 in hex is " << _HEX(45) << endl;

You will need to download the Library from http://arduiniana.org/libraries/streaming/ and place it in a subdirectory of your Sketchbook folder. 您需要从http://arduiniana.org/libraries/streaming/下载该库,并将其放在Sketchbook文件夹的子目录中。 The Menu File-Preferences will show you where that is. 菜单文件首选项将显示它的位置。

This library can also be used when outputting to LCDs. 输出到LCD时也可以使用该库。

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

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