isdigit() for characters greater than 127

Discussion in 'C++' started by Alf P. Steinbach, Oct 9, 2004.

  1. * James Gregory:
    > I read that the argument to isdigit() can be "an integer whose value is
    > representable as an unsigned char, or the value of the macro EOF.". This
    > seems to say that it should work for values greater than 127


    Yes.


    >. And in
    > Linux + GCC, it does - values between 128 and 255 are false.
    >
    > However, in Windows XP + GCC (at least some) values between 128 and 255
    > come out as true for isdigit. Someone suggested this may be because
    > Windows uses a different character set?


    Perhaps.

    The program

    #include <iostream>
    #include <iomanip> // setw
    #include <cctype> // is_digit

    int main()
    {
    using namespace std;

    for( int i = 0; i <= UCHAR_MAX; ++i )
    {
    if( isdigit( i ) )
    {
    cout << setw( 3 ) << i << setw( 2 ) << char(i) << endl;
    }
    }
    }

    compiled with g++ 3.2.3, fails to reproduce your problem, giving

    48 0
    49 1
    50 2
    51 3
    52 4
    53 5
    54 6
    55 7
    56 8
    57 9

    Why don't you check _which_ values and what the corresponding chars are
    in the characters set used (Windows specific note: use chcp command if
    this is a console program). And post the code and results. Always a
    good idea to post the code -- we're not telepaths, you know... ;-)


    > So am I not supposed to use isdigit for non-ASCII values?


    isdigit is not limited to ASCII.


    > What is the best alternative? Writing my own isdigit function?


    What is the problem?

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Oct 9, 2004
    #1
    1. Advertising

  2. I read that the argument to isdigit() can be "an integer whose value is
    representable as an unsigned char, or the value of the macro EOF.". This
    seems to say that it should work for values greater than 127. And in
    Linux + GCC, it does - values between 128 and 255 are false.

    However, in Windows XP + GCC (at least some) values between 128 and 255
    come out as true for isdigit. Someone suggested this may be because
    Windows uses a different character set? So am I not supposed to use
    isdigit for non-ASCII values? What is the best alternative? Writing my own
    isdigit function?

    Thanks,

    James
     
    James Gregory, Oct 9, 2004
    #2
    1. Advertising

  3. "James Gregory" <> wrote in message
    news:p...
    >I read that the argument to isdigit() can be "an integer whose value is
    > representable as an unsigned char, or the value of the macro EOF.". This
    > seems to say that it should work for values greater than 127. And in
    > Linux + GCC, it does - values between 128 and 255 are false.
    >
    > However, in Windows XP + GCC (at least some) values between 128 and 255
    > come out as true for isdigit. Someone suggested this may be because
    > Windows uses a different character set? So am I not supposed to use
    > isdigit for non-ASCII values? What is the best alternative? Writing my own
    > isdigit function?
    >


    Perhaps you are writing this

    char ch;
    ....
    isdigit(ch);

    when you should be writing this

    char ch;
    ....
    isdigit(static_cast<unsigned char>(ch));

    The first alternative can pass a negative value to isdigit (because char is
    signed) and that has undefined effects.

    john
     
    John Harrison, Oct 9, 2004
    #3
  4. * John Harrison:
    >
    > Perhaps you are writing this
    >
    > char ch;
    > ...
    > isdigit(ch);
    >
    > when you should be writing this
    >
    > char ch;
    > ...
    > isdigit(static_cast<unsigned char>(ch));
    >
    > The first alternative can pass a negative value to isdigit (because char is
    > signed) and that has undefined effects.


    Nitpick: "because char is signed" -> "because char may be a signed type,
    depending on the implementation".

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Oct 9, 2004
    #4
  5. "John Harrison" <> wrote in message
    news:...
    >
    > "James Gregory" <> wrote in message
    > news:p...
    > >I read that the argument to isdigit() can be "an integer whose value is
    > > representable as an unsigned char, or the value of the macro EOF.". This
    > > seems to say that it should work for values greater than 127. And in
    > > Linux + GCC, it does - values between 128 and 255 are false.
    > >
    > > However, in Windows XP + GCC (at least some) values between 128 and 255
    > > come out as true for isdigit. Someone suggested this may be because
    > > Windows uses a different character set? So am I not supposed to use
    > > isdigit for non-ASCII values? What is the best alternative? Writing my

    own
    > > isdigit function?
    > >

    >
    > Perhaps you are writing this
    >
    > char ch;
    > ...
    > isdigit(ch);
    >
    > when you should be writing this
    >
    > char ch;
    > ...
    > isdigit(static_cast<unsigned char>(ch));
    >
    > The first alternative can pass a negative value to isdigit (because char

    is
    > signed) and that has undefined effects.
    >


    This was indeed my problem.

    Thanks,

    James
     
    James Gregory, Oct 9, 2004
    #5
    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. Ryan
    Replies:
    1
    Views:
    516
  2. falcon
    Replies:
    10
    Views:
    18,698
    Roedy Green
    Feb 24, 2006
  3. hokiegal99
    Replies:
    11
    Views:
    509
    Alex Martelli
    Aug 4, 2003
  4. Replies:
    6
    Views:
    564
    Pete Becker
    May 25, 2007
  5. Dwight Army of Champions
    Replies:
    4
    Views:
    2,758
    John H.
    Mar 17, 2010
Loading...

Share This Page