October 8th, 2003, 03:41 AM
Convert to chr
hi, in C, what's the func to convert asc to chr and hex to chr?
October 8th, 2003, 05:00 AM
Taint one. In C/C++ there is no difference between an eight bit integer and a character.
To print out the value use this:
printf("%X", charVal);//hex in upper case, 'x' in lower case
To assign a value, just assign it:
int charVal = 'A';
October 8th, 2003, 05:01 AM
An attack of nasty and unnecessary abbreviation?
You probably need to be more precise in your question. There is no difference in C between a character's ACSII code and its integer value. So for example:
. If you mean, how can I get the numeric value of an ASCII decimal digit, then:
char ASCII_A = 'A' ;
. You may mean converting an ASCII string containing a number into an integer:
int digit_value = (int)( ascii_digit - '0') ;
. With respect to hex conversions, the easiest approach is to use scanf():
int value = atoi( acsii_string ) ;
. You can use
check = sscanf( hex_string, "%x", &value ) ;
to generically convert hex, decimal or octal strings based on the prefix in the string (0x for hex, 0(zero) for octal, otherwise decimal).
int value = sscanf( ascii_string, "%i", value ) ;
That's enougn guesses of what you meant, let is know if any of these is a solution, or elaborate on your requirements.