How to get decimal value of a unicode character in c++ - c++

For one of my opensource projects, i need to compute the decimal equivalent of a given unicode character.
For example if tamil character L'அ' is given, the output should be 2949 .
I am using c++ in Qt environment. I googled and could not find a solution for this. Please help if you know a solution for this.

Use the unicode() method of the QChar object (which you can get e.g. with the at method of a QString, if a QString is what you have to start with).

cout << (int)c
that's it

void print_decimal(wchar_t character)
{
std::cout << int(character);
}

Related

printing multiplication sign in c++

In Qt for setting the label of a button to be the "Multiplication sign" (not astrisk(*)) this is used :
Button *myButton = new Button("\303\227");
I don't know anything about "\303\227". Also the code below will not print "Multiplication sign" :
cout << "\303\227" ;
I could not search for it cause I had no "keywords".
I need some keywords and some information about this number and why cannot it be printed with cout object.
Also i didn't want to add Qt tag for this question cause I think this is not much Qt related.
Thanks in advance.
those are called octal codes for UTF-8 characters, and I believe C++ has no useful native Unicode support. However, you may refer to this question
for how to use unicode in qt.
if you just want the multiplication symbol you may use this “×” U+00D7 or this U+2715 ✕

Windows UTF8 to UTF16

I've been trying to convert between UTF8 and UTF16 LE with BOM using C++ to make the characters output correctly on Windows, without having to change the font of the terminal.
I tried changing the code pages, but they didn't work.
I have 2 questions,
How can I convert a normal string to a wide string?
Is it a bad idea to create a C++ map that maps each unicode character to the character in the Windows code page?
For example,
wcout << L"\u00A0" << endl;
This code outputs the letter á on Windows when using Code page 850. How can I put a variable in place of the "\u00A0" to convert a normal string to a wide character on Windows?
What I'd like is this:
wcout << Lsome_variable << endl;
I realise it's not valid c++ syntax but does anyone know how I can do this? Or if there's a better way?
As noted in the comments, the standard library now provides things like std::wstring_convert (and other functions/classes in the See Also section of that page).
Since you're on Windows, the WinAPI also has conversion functions. In this case you would be looking for MultiByteToWideChar which can be used to convert from UTF-8 to UTF-16.
Between those options, something should fit your use case. Generally speaking, you should never need to write your own conversion map.

Store an Ascii char in an array in c++

first of all thanks for reading ^ ^
ok, so I'm making an easy menu for a console project
after I created the options I wanted to add the char(240) as another option but I can't figure out how to declare it I cant just write ≡ because Dev++ won't let me write it, the code is:
char *menu_list[6] = { " SYMBOL GOES HERE "," View ", " Edit ", " Search ", " Reset", " Quit " };
does anyone know how to do this? or if I'm doin it all wrong, can you tell me the right way to do it?
i'm forced to make it work on windows, i can
cout << char(240);
and it works right, but I cannot store that same symbol into menu_list
also I got the code from here
http://www.theasciicode.com.ar/extended-ascii-code/hyphen-ascii-code-240.html
There was a deleted answer that had the correct response: use "\xf0" to represent the character.
Ordinarily you would need to know which code page is being used by Windows to know how a character code maps to a particular character on screen. However by doing the research yourself, you already know that the decimal value 240 represents the character you need. That decimal 240 is the same as hex f0, and the way to embed a hex literal value in a string is to prefix it with \x.
As noted in the link you posted, these codes correspond to code page 437. This is the one used by the command window in English versions of Windows; I'm not sure if alternate language versions of Windows use anything different. Calling this "extended ASCII" is confusing, since there have been many attempts to extend ASCII over the years and none of them are the same.

How to insert check mark "✓" in std::string in c++ programming using VS2010

In one of my application I'm developing in c++, I have to display "✓" mark. For that I need to first insert the same in a std::string or in a char. But when I do that, I'm getting a "?" mark as output. I'm using VS2010 to code. Please suggest how to solve the same. Thanks in advance.
There seems to be some basic misunderstanding.
The checkmark character is Unicode 0x2713. You cannot store it as a single character in std::string. The maximum value for a char is 0xff (255). It won't fit.
If you are developing a GUI using C++ for Windows then I would guess MFC. However if you are using std::string then perhaps that's not so. Some choices:
For MFC, you can rebuild your application in UNICODE mode. Then chars are short (16 bits) and your checkmark will fit fine.
You could use std::wstring instead of string. That means changes to existing code.
You could use UTF-8, which replaces the character by a multi-byte sequence. Not recommended in Windows, even if you think you know what you're doing. Very unfriendly.
In any case, if you are using GUI and dialog boxes you will have to make sure they are Unicode dialog boxes or nothing will work.
With a few more details, we could give more specific advice.
you can insert check mark in your console using C++ using the following code
cout << " (\xfb) "<<endl;
Output:
(√)

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 ã‚Š.
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.
The simplest way is to check every charachter's code (QChar::unicode()) to be below 128 if you need pure 7-bit ASCII.
To write it in compact way without loop, you can use regular expression:
bool containsNonASCII = myString.contains(QRegularExpression(QStringLiteral("[^\\x{0000}-\\x{007F}]")));
this works for me :
isLetterOrNumber()
ot_id += QChar((short) b.to_ulong()).isLetterOrNumber() ? QChar((short) b.to_ulong()) : QString("");