{SOLVED]Convert Integer to Hex to uint_8 (not in a array)

Hi !

I've a problem, and I searched a lot to find a answer ... but nothing.

So for my program, I need to convert an Int in Hex to an uint_8.
I found a solution with using a String to convert the Int var in Hexa, like this :

String str = String(intValue, HEX);

But I can't convert the String to one uint_8 var, (but it's possible in a array) .

Have you a different solution ?
Thanks for answers,

Sorry for my English :slight_smile:

Well, something like:

int var1 = 0x007E;
uint_8 var2 = (uint_8) var1;

must work. But is not what you really think it is.

Thanks for this fast answer :slight_smile:

Thats a good solution but I need to convert a Int to a hex value, like this :

int var = 5;
uint_8 var2;

var2 = var in hexadecimal

So if we can convert a int to hex in another int, that's not a problem

You don't need to! That's why I wrote:

luisilva:
(...)
But is not what you really think it is.

The value, if you want to say it like that, is already in HEX. The HEX is only a way to show the value to a Human been. I know that maybe is hard to understand, but in computers (and Arduino is a computer) all the information is stored in binary form. HEX is only a way to show binary (as well as DEC).

If you want, you can do something like:

  Serial.println(var2, HEX);

and the value is shown in hexadecimal, or you can do:

  Serial.println(var2, DEC);

and the value is shown in decimal, or even:

  Serial.println(var2, BIN);

But the value is always the same.

So I understand what do you mean . (Today I learn a new thing :smiley: )

But I wanna explain a bit more what is my program and perhaps you'll understand what I need this value in HEX.

I'm making a physical keyboard with arduino.
To send a keystroke to the Computer, you need follow the HID reference table, it's like the ASCII table, but for computer keystrokes (see page 53 in the attached file).

This is a small program to send a keystroke (after flashed arduino with special frimware to handle this functionality) :

int chrA = 4 //HID value for A
uint8_t key[8] = {0,0,0,0,0,0,0,0};

void setup()
{
    Serial.begin(9600);
    key[2] = chrA;
}

void loop()
{
   Serial.write(key, 8); 
   
   delay(1000); // Prevent from spam
}

I use a uint8_t array because I need it to handle specials characters or SHIFT, CTRL, ...

As you see, I didn't need to convert 4 in HEX, because 4 in HEX = 4.
But, e.g., to send a "W" keystroke (decimal value = 27, see HID table), we need to convert 27 in HEX = 1A.

So maybe you understand why I need a HEX value.

Hut1_11.pdf (880 KB)

As luisilva said, computers always store numbers in binary and always communicate between each other in binary. a Hex representation is merely there for YOU to communicate to the computer.

You could write it in many ways, because the compiler makes it easy for you, and converts what you want to what the computer needs:

int chrW = 26;
int chrWAsWell = 0x1A;
int chrWAlso = B00011010;

and you will find that chrW is equal to chrWAsWell is equal to chrWAlso, because they all represent the Decimal number 26, and if you send any one of these three variables as a keystroke in your setup, they will all three simulate the 'w' button being pushed, because they are all the same value.

EDIT - Fixed binary constant - thanks luisilva

uint8_t charW = 27

and

uint8_t charW = 0x1A

are exactly identical. There is literally NO DIFFERENCE in the code these two statements will produce.

So, I realized that I've not really undertand what luisilva says before now.

I test my program with sending 28 (HID value for W), and thats work !

So thanks for your answers guys :stuck_out_tongue: