Thread: Convert to chr

    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    76
    Rep Power
    12

    Convert to chr


    hi, in C, what's the func to convert asc to chr and hex to chr?
    Thanks
  2. #2
  3. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    244
    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';

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  4. #3
  5. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,111
    Rep Power
    1803
    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

IMN logo majestic logo threadwatch logo seochat tools logo