convert int to char

This is a discussion on convert int to char within the C Programming forums, part of the General Programming Boards category; hi, i've got to integers, lets say 0 and 6, which i want to copy into an character array. how ...

  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
    B26354 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
    B26354 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,796
    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, 04: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, 09:36 PM
  3. Replies: 8
    Last Post: 03-10-2008, 12:57 PM
  4. How do i un-SHA1 hash something..
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 09-14-2005, 06: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21