Thread: How to convert a int to char?

  1. #1
    TheGr8one
    Guest

    How to convert a int to char?

    Does anyone know a way to convert an int to a char so if I have a int with the value of 45, I could convert it to a char with the value "45", anyone?

    Thanks
    Chris

  2. #2
    Registered User bljonk's Avatar
    Join Date
    Oct 2001
    Posts
    70

    Question (casting)cast;

    Use the cast operator:

    #include<stdio.h>

    int main(void) {
    int x;
    char y
    scanf("%x", &x);
    y = (char)x;
    printf("here is the eq. character: %c\n", y);
    ....
    return 0;
    }
    there are certain rules about casting only certain types can be passed to another and other that can be casted to all the others.
    Ünicode¬>world = 10.0£

  3. #3
    Registered User bljonk's Avatar
    Join Date
    Oct 2001
    Posts
    70

    Wink I C'ed not C++'ed

    sorry about that sub. header files and stream functions:
    printf() and scanf() for cout and cin respectively.

    y = (typr) x;
    Ünicode¬>world = 10.0£

  4. #4
    TheGr8one
    Guest
    with multicasting it gives the number a char value and what im tryin to do is put the actuall number from the int and put the same value from int into a char.

    Thanks
    Chris

  5. #5
    LuckY
    Guest

    Post

    Well, in case you're unaware, you can't convert int into char as the int may have more than one character.
    To turn an int into a char* you can use itoa() (in stdlib.h I believe), but in MSVC it's _itoa().
    You use it like so:

    char buf[10];//buffer to output to
    int num = 45;//source value
    itoa(num,buf,10);
    //10 == radix (in other words, the base ie: 2=binary, 16=hex, 8=octal)

    HTH.

  6. #6
    Unregistered
    Guest
    lol...TheGr8one signs just like me, exept I put a dash before my name

    Thanks
    -Chris

  7. #7
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    Also wanted to mention, itoa means "Int TO Ascii"

  8. #8
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    oops, I forgot to sign in when I posted the above message

    Thanks
    -Chris

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  2. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  3. Replies: 8
    Last Post: 03-10-2008, 11:57 AM
  4. How do i un-SHA1 hash something..
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 09-14-2005, 05:59 AM
  5. easy if you know how to use functions...
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 01-31-2002, 07:34 AM