How can i convert an integer value into its corresponding hex and store it in a single byte variable using C language.
int nVar = 24; // where hex value 0x18
BYTE byRes;
char sBuff[8] = {0};
sprintf(sBuff, "%x", nVar);
Where sBuff[0] = 0x30 and sBuff[1] = 0x38 but this is not what i am looking for. I want to save the hex value of nVar in byRes variable.
Thanks
Note that “decimal” or “hex” is only a representation of an numeric value. In other words, these are different ways of displaying an
inton the screen. AssigningbyRes = (BYTE)nVar;copies the value ofnVartobyRes. You can display that value as hex or decimal or base-7 or base-42 if you want. Modern computers store data as differences in voltage. At the machine level, we often think of these as in terms of “high” and “low” or “0” and “1” (aka binary), but at the programming level, we can think of these numeric values any way we wish. Assigning a “hex” value from one variable to another is the same as assigning the “decimal” value because the computer does not know how we think about the value until we ask it to display it. When you do this (e.g. with aprintf()call), the computer converts the values to ASCII characters which are no longer really the integral value which we are manipulating.