int to char?

This is a discussion on int to char? within the C Programming forums, part of the General Programming Boards category; Hi there I have a variable of type char[10] where i will always store a Character in char[0], however 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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    if you want a character represnetation of the number like '7' instead of just number 7 use
    int num = 7;
    var[1] = '0' + num;
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    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?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

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, 03: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

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