Convert integer / decimal to hex on Arduino?

How can I convert an integer or decimal variable to a hexadecimal string? I can do the opposite (convert hex to int), but I cannot figure out another way.

This is for Serial.print()hexadecimal values ​​in an array.

+5
source share
3 answers

Take a look at the Arduino String tutorial here . The code below was taken from this 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);  

Many other examples are presented on this page, although I think for floating point numbers you will have to collapse your own.

+14
source

There is a simple solution, just use:

Serial.print(yourVariable, HEX);
+7
source

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. Menu file preferences will show you where it is.

This library can also be used for output to LCD displays.

+2
source

All Articles