Reputation: 7928
I'm building an MFC project in C++. I have a text field, in which the user is expected to type a number. I can convert it to integer when an English keyboard is used. But when the user switches to a Japanese keyboard, it doesn't work.
I know that the Japanese keyboard creates double-byte characters. But I don't know how to convert them to integer.
Sory for lacking of information.
My intention is check whether the input user types in text field is a number or not. But it doesn't recognize the number with Japanese keyboard. I tried some methods, such as: strtol(), _wtoi. But the result (frequency) is always 0; The string I got when debuging is 123456789 instead of 123456789. Here is my code
CString strFrequency;
GetDlgItem(IDC_EDIT_FREQUENCY)->GetWindowText(strFrequency);
if(strFrequency.IsEmpty()){
CDialog::OnOK();
return;
}
for(int i=1; i<strFrequency.GetLength(); i+=2) {
if(!std::isdigit(strFrequency[i])){
MessageBox("数字で入力してください。","発表支援");
return;
}
}
int frequency = atoi(strFrequency);
Can everybody help me?
Thanks in advance
Upvotes: 0
Views: 976
Reputation: 7928
CString strFrequency;
GetDlgItem(IDC_EDIT_FREQUENCY)->GetWindowText(strFrequency);
DWORD flags = LCMAP_HALFWIDTH;
const int size = strFrequency.GetLength() / 2 + 1;
char* s = new char[size];
ZeroMemory(s, size);
LCMapString(LOCALE_SYSTEM_DEFAULT,
flags,
strFrequency,
strFrequency.GetLength() + 1,
s,
size);
CString convertedText = s;
I solved it.The string (strFrequency) is typed by Japanese keyboard is full-width length. So I converted a full-width string to half-width string. Then I can check whether it is a numeric string or not.
Upvotes: 1
Reputation: 145359
If this call compiles and does present the japanese characters,
MessageBox("数字で入力してください。","発表支援");
then it seems you're building your project as ANSI, not as UNICODE.
Try building your project as UNICODE, changing the call to
MessageBox(L"数字で入力してください。",L"発表支援");
Upvotes: 0
Reputation: 33546
Without seeing your code that tries to convert the number, and without at least a loose description of what actually happens when you put japanese text into it, it will be quite hard to tell what/why is wrong. For now, the only thing I can think of is that you either are using wrong datatype (i.e. CHAR instead of WCHAR) or maybe you are trying to parse japanese symbols like '四万' directly with some arabic-only parser like atoi(), or maybe you forgot to set stream encoding or locale properly.
Please have in mind that while japanese keyboard and japanese locale will be able to produce text of '40000' that probably will be parseable with istringstream (and maybe even will by some atoi version), then almost surely the japanese digits like '四万' (that's also 40000) most probably will not be parsed. I think that '四万' is clasified as text not digits - but I may be very wrong here! I have never tried it. But, after searching a bit I don't see any mention that this is possible.
However, there seem to be some ways to parse kanji digits too, see for example How to parse kanji numeric characters using ICU?
Upvotes: 0