Int 2 Char

This is a discussion on Int 2 Char within the C++ Programming forums, part of the General Programming Boards category; I am new to C++, can anyone kindly tell me how I can transfer the value of an Int variable ...

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    24

    Lightbulb Int 2 Char

    I am new to C++, can anyone kindly tell me how I can transfer the value of an Int variable to a Char Variable.

    If int X=55, I want char Z=55,
    I have tried Z=(char)X;
    but it doesn't works.

    Please Help.

  2. #2
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,825
    Well, what do you mean exactly? Do you want Z to equal the ASCII value 55 or Z to be a string "55"?

  3. #3
    ResurgentBarbecue UnclePunker's Avatar
    Join Date
    May 2002
    Posts
    128
    I'm pretty sure you would have to convert from an integer to a string, I think that is because integers are 2 bytes and characters are just 1 byte, I think.

    There is a command called itoa, it stands for integer to alpha, I think it is a function in stdlib.h, have a look on your help.

    This is just an idea but I think it should work, I haven't compiled it.

    Code:
    #include <iostream.h>
    #include <stdlib.h>
    
    int main()
    {
        int x = 55;
        char z[5];
    
        cout<<x;
    
        itoa(x, z, 5);
    
        cout<<z;
    
    return 0;
    }
    you pass the itoa function the name of the integer variable the name of the string and the length of the string, I hope this works and helps.
    Compiler == Visual C++ 6.0
    "Come Out Fighting."

  4. #4
    Registered User
    Join Date
    Jul 2002
    Posts
    273
    Originally posted by UnclePunker
    I'm pretty sure you would have to convert from an integer to a string, I think that is because integers are 2 bytes and characters are just 1 byte, I think.
    It's because there are no letters in the range of integers. ASCII is a set of characters mapped to numbers. What the poster wants is to take the ASCII representation of those numbers. Actually stored much differently than the original.

    "A" = 65
    "2" = 50
    "35" = 51, 53

    etc...
    Last edited by Hershlag; 07-15-2002 at 07:54 AM.

  5. #5
    Registered User
    Join Date
    Jul 2002
    Posts
    24

    Lightbulb Thanx

    Thanx for your Mails.
    You are right I do wish to convert the integer to string.
    & I will try out your advise.

  6. #6
    ResurgentBarbecue UnclePunker's Avatar
    Join Date
    May 2002
    Posts
    128
    I guess your right about the ASCII values, but am I not right that integers are 2 bytes and characters are 1 byte, and I think he did actually want the z character variable to print out 55, I did a similar exercise when I had just started a few months back.
    Compiler == Visual C++ 6.0
    "Come Out Fighting."

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: 14
    Last Post: 06-28-2006, 02: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, 02:27 PM

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