Thread: convert int to char

  1. #1
    Unregistered
    Guest

    convert int to char

    hi,
    i've got to integers, lets say 0 and 6, which i want to copy into an character array. how do i do this?

    char copied[3];
    //copied[0] should be 0
    //copied[1] should be 6
    //copied[2] should be '/0' - the terminating character.

    any suggestions?

    tom

  2. #2
    The Artful Lurker Deckard's Avatar
    Join Date
    Jan 2002
    Posts
    633
    Well, a character is little more than an integer with a smaller range. The following will work:
    Code:
    copied[0] = 0;
    copied[1] = 6;
    copied[3] = 0;
    Note that '\0' and 0 are the same value. This may cause problems for you with copied[0]. If you want the ASCII characters zero and six (and not the numeric values zero and six), try:
    Code:
    copied[0] = '0';
    copied[1] = '6';
    copied[3] = 0; /* same as '\0' */
    Jason Deckard

  3. #3
    The Artful Lurker Deckard's Avatar
    Join Date
    Jan 2002
    Posts
    633
    I also have the feeling I've misunderstood your question. If you want to take numeric values and put their ASCII equivalent in a string, look into sprintf().
    Code:
    sprintf( my_string, "The number is %d\n", my_number );
    Jason Deckard

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Don't cross post please.

    -Prelude
    My best code is written with the delete key.

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