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?
Thanks
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("%d", charVal);//integer
printf("%c", charVal);//character
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:
Code:
char ASCII_A = 'A' ;
. If you mean, how can I get the numeric value of an ASCII decimal digit, then:
Code:
int digit_value = (int)( ascii_digit - '0') ;
. You may mean converting an ASCII string containing a number into an integer:
Code:
int value = atoi( acsii_string ) ;
. With respect to hex conversions, the easiest approach is to use scanf():
Code:
check = sscanf( hex_string, "%x", &value ) ;
. You can use
Code:
int value = sscanf( ascii_string, "%i", 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).
That's enougn guesses of what you meant, let is know if any of these is a solution, or elaborate on your requirements.
Clifford