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:
So I understand what do you mean . (Today I learn a new thing )
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.
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.