Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert integer/decimal to hex on an Arduino?

Tags:

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.

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

like image 565
Joe Avatar asked Apr 18 '11 12:04

Joe


2 Answers

Take a look at the Arduino String tutorial here. 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.

like image 63
nathan Avatar answered Oct 23 '22 02:10

nathan


There's a simple solution, just use:

Serial.print(yourVariable, HEX);
like image 25
Rodrigo Lopez Avatar answered Oct 23 '22 01:10

Rodrigo Lopez