473,406 Members | 2,707 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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
Jul 22 '05 #1
4 3590
* 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?
Jul 22 '05 #2

"James Gregory" <j.**@virgin.net> wrote in message
news:pa****************************@virgin.net...
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
Jul 22 '05 #3
* 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?
Jul 22 '05 #4

"John Harrison" <jo*************@hotmail.com> wrote in message
news:2s*************@uni-berlin.de...

"James Gregory" <j.**@virgin.net> wrote in message
news:pa****************************@virgin.net...
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
Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Carramba | last post by:
hi! I am trying to confirm if input is digit, so I thought it would by easy to do it with with isdigit() funktion, but how do I pass arrays to it if the imput is more then 1 sign? #include...
7
by: Per Rollvang | last post by:
Hi All! I have a problem with hex strings when using Char.IsDigit(). The hex strings(i.e. a CRC32 checksum) get a false result for letters A-F... MSDN tell me that: Indicates whether a...
2
by: Jefe | last post by:
Hi Group Could you please explain to me what's the difference between Chars.IsDigit and Chars.IsNumber? Regards,
0
by: Michael Mallete | last post by:
good day everyone! i am using postgresql-8.0beta2-dev3 windows port, with postgis-0.9 add-on. anyway, while trying to insert a table using shp2pgsql, i get this error: psql:temp.sql:38:...
10
by: mdh | last post by:
Could I get some help as to understanding why I am not getting a result I expect. #include <stdio.h> #include <ctype.h> int main (){ int i,j,k,c; i=9;
3
by: MooMaster | last post by:
N00b question alert! I did a search for isdigit() in the group discussion, and it didn't look like the question had been asked in the first 2 pages, so sorry if it was... The manual...
4
by: Jonathan | last post by:
Hi, Can you please tell me how can I printf 'n' characters of from a char* to a file. I notice there is a snprinf, but I don't see one corresponding to fprintf? I am thinking if i can do...
13
by: Liang Chen | last post by:
Hope you all had a nice weekend. I have a question that I hope someone can help me out. I want to run a Python program that uses Tkinter for the user interface (GUI). The program allows me to type...
2
by: bozo789 | last post by:
Hi Forum, First let me begin by stating I am a student and have read the proper posting procedure concerning students. I am a begginer learning with the Miracle C Work Bench. I know my code is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.