Thread: int to char?

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    32

    int to char?

    Hi there

    I have a variable of type char[10] where i will always store a Character in char[0], however char [1] will always be a number between 0 and 9.

    therefore Z9 is one possibility of how this can occur... Apart the letter part I solved it... however im filling char[1] with junk for some reason i dont know :S

    i was trying

    Code:
      num = 7; 
      var[1] =(char)num;
    when this happened :S any suggestions? btw i dont want to use sprintf

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    if you want a character represnetation of the number like '7' instead of just number 7 use
    int num = 7;
    var[1] = '0' + num;
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Mar 2007
    Posts
    4
    The character '7' is not represented as 7 in ASCII.

    EDIT: Too late.

  4. #4
    Registered User
    Join Date
    Apr 2007
    Posts
    32
    ok sorted it out

    Thanks man u've been of great help to me

  5. #5
    Linux is where it's at movl0x1's Avatar
    Join Date
    May 2007
    Posts
    72
    Can't you use a struct?

    Code:
    struct var{
         char[9];
         int;
    };

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by movl0x1 View Post
    Can't you use a struct?

    Code:
    struct var{
         char[9];
         int;
    };
    Actually - this struct is possible to use somewhere... But what it has to do with the OP's problem?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

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: 14
    Last Post: 06-28-2006, 01:58 AM
  4. Game Won't Compile
    By jothesmo in forum C++ Programming
    Replies: 2
    Last Post: 04-01-2006, 04:24 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM