casting int to char

Discussion in 'C++' started by Eric, Nov 9, 2006.

  1. Eric

    Eric Guest

    Is there a simple, standard statement that will reinterpret an int in
    the range of 0-9 as a char? I understand it's simple enough to write in
    a function, but I wonder if there's a more general approach.
    Eric, Nov 9, 2006
    #1
    1. Advertising

  2. Eric

    Gavin Deane Guest

    Eric wrote:
    > Is there a simple, standard statement that will reinterpret an int in
    > the range of 0-9 as a char? I understand it's simple enough to write in
    > a function, but I wonder if there's a more general approach.


    If you add an int in the range 0 to 9 to the char '0' you are
    guaranteed to get the appropriate character in the range '0' to '9'.

    #include <iostream>
    using namespace std;

    int main()
    {
    char c = '0';
    for (int i = 0; i <= 9; ++i)
    cout << static_cast<char>(c + i) << "\n";
    }

    Does that answer your question?

    Gavin Deane
    Gavin Deane, Nov 9, 2006
    #2
    1. Advertising

  3. Eric

    Jim Langston Guest

    "Eric" <> wrote in message
    news:eivohr$697$...
    > Is there a simple, standard statement that will reinterpret an int in the
    > range of 0-9 as a char? I understand it's simple enough to write in a
    > function, but I wonder if there's a more general approach.


    A character is number (are they called scalars? I forget) and as such you
    can do math on them.

    I take it you have a number such as 0 and you want to make this become the
    character '0'. This is fairly simple since they are both types of numbers,
    you can just add them. If you add 0 to the character '0' you get the value
    for the character '0'. Add 1 and you get the value for the character '1',
    etc...

    So,

    int MyNumber = 5;
    char MyChar = '0' + MyNumber;
    MyChar will now contain the character '5'.

    If you are trying to do something else plese explain.
    Jim Langston, Nov 9, 2006
    #3
  4. Eric

    BobR Guest

    Eric wrote in message ...
    >Is there a simple, standard statement that will reinterpret an int in
    >the range of 0-9 as a char? I understand it's simple enough to write in
    >a function, but I wonder if there's a more general approach.


    In case the other answers were not what you wanted:

    int num( 9 );
    char cnum( num );
    or insure the int will fit in a char:
    num &= 0x7F;
    char cnum2( num );
    or if you need to assign:
    cnum = static_cast<char>( num );

    Be aware that 'char' may be 'unsigned char' or 'signed char' depending on
    implementation/OS.
    You can check which is used (among other ways):

    #include <limits>
    {
    std::cout <<"numeric_limits<char>::max() ="
    <<int(std::numeric_limits<char>::max())<<std::endl;
    std::cout <<"numeric_limits<char>::min() ="
    <<int(std::numeric_limits<char>::min())<<std::endl;

    std::cout <<"numeric_limits<unsigned char>::max() ="
    <<int(std::numeric_limits<unsigned char>::max())<<std::endl;
    std::cout <<"numeric_limits<unsigned char>::min() ="
    <<int(std::numeric_limits<unsigned char>::min())<<std::endl;
    }

    If the outputs match, 'char' == 'unsigned char'.

    [ if you knew all this already, sorry. Maybe it might help some newbie.]
    --
    Bob R
    POVrookie
    BobR, Nov 9, 2006
    #4
  5. Eric

    peter koch Guest

    BobR skrev:
    [snip]
    >
    > Be aware that 'char' may be 'unsigned char' or 'signed char' depending on
    > implementation/OS.


    This answer is at best misleading. C++ has three character types:
    unsigned char, signed char and (plain) char.

    /Peter
    [snip]
    peter koch, Nov 9, 2006
    #5
  6. Eric

    Nate Barney Guest

    Gavin Deane wrote:
    > Eric wrote:
    > > Is there a simple, standard statement that will reinterpret an int in
    > > the range of 0-9 as a char? I understand it's simple enough to write in
    > > a function, but I wonder if there's a more general approach.

    >
    > If you add an int in the range 0 to 9 to the char '0' you are
    > guaranteed to get the appropriate character in the range '0' to '9'.


    Doesn't that depend on the character set you're using? I know ASCII
    has the digits in a contiguous range, and I believe EBCDIC does as
    well, but it is conceivable that a character set could be devised that
    does not have this property.

    Nate
    Nate Barney, Nov 9, 2006
    #6
  7. Eric

    BobR Guest

    peter koch wrote in message ...
    >
    >BobR skrev:
    >[snip]
    >>
    >> Be aware that 'char' may be 'unsigned char' or 'signed char' depending on
    >> implementation/OS.

    >
    >This answer is at best misleading. C++ has three character types:
    >unsigned char, signed char and (plain) char.
    >
    >/Peter


    Thanks for cleaning up that poor wording on my part.

    "I know you believe you understand what you think I said, but, I'm not sure
    you realize that what you heard is not what I meant!"
    --
    Bob R
    POVrookie
    BobR, Nov 9, 2006
    #7
  8. Eric

    Default User Guest

    Nate Barney wrote:

    > Gavin Deane wrote:
    > > Eric wrote:
    > > > Is there a simple, standard statement that will reinterpret an
    > > > int in the range of 0-9 as a char? I understand it's simple
    > > > enough to write in a function, but I wonder if there's a more
    > > > general approach.

    > >
    > > If you add an int in the range 0 to 9 to the char '0' you are
    > > guaranteed to get the appropriate character in the range '0' to '9'.

    >
    > Doesn't that depend on the character set you're using? I know ASCII
    > has the digits in a contiguous range, and I believe EBCDIC does as
    > well, but it is conceivable that a character set could be devised that
    > does not have this property.


    It's required by the standard. Under section 2.2 (Character Sets) part
    3:

    In both the source and execution basic character sets, the value of
    each character after 0 in the above list of decimal digits shall be one
    greater than the value of the previous.





    Brian
    Default User, Nov 9, 2006
    #8
  9. Eric

    Gavin Deane Guest

    Default User wrote:
    > Nate Barney wrote:
    >
    > > Gavin Deane wrote:
    > > > Eric wrote:
    > > > > Is there a simple, standard statement that will reinterpret an
    > > > > int in the range of 0-9 as a char? I understand it's simple
    > > > > enough to write in a function, but I wonder if there's a more
    > > > > general approach.
    > > >
    > > > If you add an int in the range 0 to 9 to the char '0' you are
    > > > guaranteed to get the appropriate character in the range '0' to '9'.

    > >
    > > Doesn't that depend on the character set you're using? I know ASCII
    > > has the digits in a contiguous range, and I believe EBCDIC does as
    > > well, but it is conceivable that a character set could be devised that
    > > does not have this property.

    >
    > It's required by the standard. Under section 2.2 (Character Sets) part
    > 3:
    >
    > In both the source and execution basic character sets, the value of
    > each character after 0 in the above list of decimal digits shall be one
    > greater than the value of the previous.


    Which standard are you quoting? 2.2/3 in the 1998 C++ standard doesn't
    say that.

    While I have seen enough independent sources state that this
    requirement is standard to be confident restating it to the OP in this
    thread, I don't think I have ever seen it in a standard for myself. I
    have wondered whether that is because C++ inherits the requirement from
    the C standard.

    Gavin Deane
    Gavin Deane, Nov 10, 2006
    #9
  10. Eric

    Pete Becker Guest

    Gavin Deane wrote:
    >
    > Which standard are you quoting? 2.2/3 in the 1998 C++ standard doesn't
    > say that.
    >


    The 1998 standard was replaced by the 2003 standard.

    --

    -- Pete
    Roundhouse Consulting, Ltd. -- www.versatilecoding.com
    Author of "The Standard C++ Library Extensions: a Tutorial and
    Reference." For more information about this book, see
    www.petebecker.com/tr1book.
    Pete Becker, Nov 10, 2006
    #10
  11. Eric

    Default User Guest

    Gavin Deane wrote:

    >
    > Default User wrote:


    > > It's required by the standard. Under section 2.2 (Character Sets)
    > > part 3:


    > Which standard are you quoting? 2.2/3 in the 1998 C++ standard doesn't
    > say that.


    The one I have:

    INTERNATIONAL ISO/IEC
    STANDARD 14882
    Second edition
    2003-10-15
    Programming languages - C++
    Langages de programmation - C++


    > While I have seen enough independent sources state that this
    > requirement is standard to be confident restating it to the OP in this
    > thread, I don't think I have ever seen it in a standard for myself. I
    > have wondered whether that is because C++ inherits the requirement
    > from the C standard.


    I'd be rather surprised if it's not in the 1998 standard. It was in the
    original C standard.



    Brian
    Default User, Nov 10, 2006
    #11
  12. Eric:

    > Is there a simple, standard statement that will reinterpret an int in
    > the range of 0-9 as a char? I understand it's simple enough to write in
    > a function, but I wonder if there's a more general approach.


    Assuming "c" is an L-value char, and that "i" is an int:

    c = (assert(i>=0 && i<=9),'0'+i);

    In Release Mode, this will become:

    c = ((void)0,'0'+i);

    --

    Frederick Gotham
    Frederick Gotham, Nov 12, 2006
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Schnoffos
    Replies:
    2
    Views:
    1,206
    Martien Verbruggen
    Jun 27, 2003
  2. trey

    newbie: char* int and char *int

    trey, Sep 10, 2003, in forum: C Programming
    Replies:
    7
    Views:
    401
    Irrwahn Grausewitz
    Sep 10, 2003
  3. Hal Styli
    Replies:
    14
    Views:
    1,625
    Old Wolf
    Jan 20, 2004
  4. lovecreatesbeauty
    Replies:
    1
    Views:
    1,034
    Ian Collins
    May 9, 2006
  5. gert
    Replies:
    20
    Views:
    1,154
Loading...

Share This Page