converting a "character" into hexadecimal
-
hi friends, i'm reading one "character" at time from RS-232 interface and storing it in a character array.I'd like to know whether any in-built functions are there to convert this "single character" into its ASCII hexadecimal eqivalent?? Thanks in Advance... Rajeev :)
-
hi friends, i'm reading one "character" at time from RS-232 interface and storing it in a character array.I'd like to know whether any in-built functions are there to convert this "single character" into its ASCII hexadecimal eqivalent?? Thanks in Advance... Rajeev :)
rajeev82 wrote:
in-built functions are there to convert this "single character" into its ASCII hexadecimal eqivalent??
Hexadecimal equivalent is not something very clear. What do you want to do ? Print this value in a string (using its hexa representation) ? If yes, you can use printf with the 'x' tag:
char szString[50];
char Temp = 'A';
sprintf(szString,"%x",Temp);Because a number is neither hexa, decimal nor binary, it's just its representation that can change. EDIT: changed printf to sprintf, thanks David :)
Cédric Moonen Software developer
Charting control -
hi friends, i'm reading one "character" at time from RS-232 interface and storing it in a character array.I'd like to know whether any in-built functions are there to convert this "single character" into its ASCII hexadecimal eqivalent?? Thanks in Advance... Rajeev :)
rajeev82 wrote:
...are there to convert this "single character" into its ASCII hexadecimal eqivalent??
For what purpose? Usually when someone talks of converting from base-10 to base-16, it is for display purposes. When a number is stored, its base is irrelevant. In other words, 123, 0x7b, and 1111011 all represent the same number.
"The largest fire starts but with the smallest spark." - David Crow
"Judge not by the eye but by the heart." - Native American Proverb
-
hi friends, i'm reading one "character" at time from RS-232 interface and storing it in a character array.I'd like to know whether any in-built functions are there to convert this "single character" into its ASCII hexadecimal eqivalent?? Thanks in Advance... Rajeev :)
I was about to ask if you are working for the company I use to work for (the only one I know that uses RS-232 and transmits data at twice its size), but then I saw you are in India ... Anyway, to answer your question: To convert a number to a text-readable ASCII-Hex representation (that is, each half of a byte is represented by a single hexadecimal character [0-9aA-fF]), use the printf family:
char buffer[100] = {0}; BYTE myInfo = 64; sprintf(buffer, "%X", myInfo);
To convert an ASCII-Hex number back to a byte, use sscanf:char* buffer = "AB"; BYTE myInfo = 0; sscanf(buffer, "%2.2X", &MyInfo);
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac -
hi friends, i'm reading one "character" at time from RS-232 interface and storing it in a character array.I'd like to know whether any in-built functions are there to convert this "single character" into its ASCII hexadecimal eqivalent?? Thanks in Advance... Rajeev :)
Perhaps this helps?
BYTE HexToByte(LPCSTR cpHex)
{
BYTE cRetVal;if (*cpHex < 58)
cRetVal = *cpHex - '0';
else
cRetVal = *cpHex - '7';cRetVal <<= 4;
cpHex++;
if (*cpHex < 58)
cRetVal += *cpHex - '0';
else
cRetVal += *cpHex - '7';return cRetVal;
}onwards and upwards...