isdigit() for characters greater than 127

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • James Gregory

    isdigit() for characters greater than 127

    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
  • Alf P. Steinbach

    #2
    Re: isdigit() for characters greater than 127

    * James Gregory:[color=blue]
    > 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[/color]

    Yes.

    [color=blue]
    >. 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?[/color]

    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... ;-)

    [color=blue]
    > So am I not supposed to use isdigit for non-ASCII values?[/color]

    isdigit is not limited to ASCII.

    [color=blue]
    > What is the best alternative? Writing my own isdigit function?[/color]

    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?

    Comment

    • John Harrison

      #3
      Re: isdigit() for characters greater than 127


      "James Gregory" <j.rg@virgin.ne t> wrote in message
      news:pan.2004.1 0.09.05.47.41.3 47498@virgin.ne t...[color=blue]
      >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?
      >[/color]

      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


      Comment

      • Alf P. Steinbach

        #4
        Re: isdigit() for characters greater than 127

        * John Harrison:[color=blue]
        >
        > 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.[/color]

        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?

        Comment

        • James Gregory

          #5
          Re: isdigit() for characters greater than 127


          "John Harrison" <john_andronicu s@hotmail.com> wrote in message
          news:2spd4sF1o3 3j9U1@uni-berlin.de...[color=blue]
          >
          > "James Gregory" <j.rg@virgin.ne t> wrote in message
          > news:pan.2004.1 0.09.05.47.41.3 47498@virgin.ne t...[color=green]
          > >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[/color][/color]
          own[color=blue][color=green]
          > > isdigit function?
          > >[/color]
          >
          > 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[/color]
          is[color=blue]
          > signed) and that has undefined effects.
          >[/color]

          This was indeed my problem.

          Thanks,

          James


          Comment

          Working...