I want to detect if the user has inputted a non-ASCII (otherwise incorrectly known as Unicode) character (for example, り) in a file save dialog box. As I am using Qt, any non-ASCII characters are properly saved in a QString, but I can't figure out how to determine if any of the characters in that string are non-ASCII before converting the string to ASCII. That character above ends up getting written to the filesystem as り
.

- 2,383
- 3
- 20
- 33
-
1Once I had an isAscii() proposal, but thiago did not like it. You could have written "if (!myString.at(x).isAcii()); then... I guess you will eventually need to use the low-level isascii, etc yourself. – László Papp Jan 07 '14 at 20:36
-
Do you mean that creating a file with name り makes it save as り? If so what are you using to create the file? – Nazar554 Jan 07 '14 at 20:37
-
2Well, ASCII characters are Unicode, but that's being a bit pedantic. For your purposes, any character with a value greater than 127 (0x7F) is "Unicode". – Hot Licks Jan 07 '14 at 20:40
-
But note that you can choose to write files as UTF8, in which case (if later read with the same attribute) all characters will be preserved. – Hot Licks Jan 07 '14 at 20:55
4 Answers
The simplest way is to check every charachter's code (QChar::unicode()) to be below 128 if you need pure 7-bit ASCII.

- 2,732
- 1
- 18
- 19
There is no such a built-in feature in my understanding.
About 1-2 years ago, I was proposing an isAscii() method for QString/QChar to wrap the low-level Unix isacii() and the corresponding Windows function, but it was rejected. You could have written then something like this:
bool isUnicode = !myString.at(3).isAcii();
I still think this would be a handy feature if you can convince the maintainer. :-)
Other than that, you would need to check against the ascii boundary yourself, I am afraid. You can do this yourself as follows:
bool isUnicode = myChar.unicode() > 127;
See the documentation for details:
ushort QChar::unicode () const
This is an overloaded function.

- 1
- 1

- 51,870
- 39
- 111
- 135
To write it in compact way without loop, you can use regular expression:
bool containsNonASCII = myString.contains(QRegularExpression(QStringLiteral("[^\\x{0000}-\\x{007F}]")));

- 41
- 1
this works for me : isLetterOrNumber()
ot_id += QChar((short) b.to_ulong()).isLetterOrNumber() ? QChar((short) b.to_ulong()) : QString("");

- 235,170
- 19
- 170
- 241

- 9
- 1
-
1As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Sep 24 '21 at 04:05