Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can non-ASCII characters be detected in a QString?

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 ã‚Š.

like image 273
Roderick Avatar asked Jan 07 '14 20:01

Roderick


People also ask

How can you tell if a character is ASCII?

A character is said to be in the range of ASCII if it is a number from 0 to 9 or a letter A to Z, or if it's some special character. We can use the is_ascii() method to check if a character is within the ASCII range.

What is an example of a non-ASCII character?

An example of a non-ASCII character is the Ñ. The URL can't contain any non-ASCII character or even a space.

Can JSON contain non-ascii characters?

JSON allows for both escaped or non-escaped non-ascii characters.


1 Answers

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.

like image 75
lpapp Avatar answered Oct 06 '22 01:10

lpapp