How to check if a unicode character is within given range in C?

耗尽温柔 提交于 2021-01-29 22:02:02

问题


The following function was written for java and has been adapted for C.

bool isFullwidthKatakana(WideChar C)
{
  return(('\u30a0'<=C)&&(C<='\u30ff'));
}

The problem is that my framework ("CodeGear C++Builder") shows this error:

[BCC32 Warning] Unit1.cpp(101): W8114 Character represented by universal-character-name '\u30a0' cannot be represented in the current code page (1252)

and it does not return true whether the conditions are met.

For example one input is 'ア' (0x30A2).

What should I do? How can I change the code page?

Thank you to the three answers they all resolved it.

return((0x30a0<=C)&&(C<=0x30ff));

It seems the that the expression \u30a0 wasn't correct, this all were correct

return((0x30a0<=C)&&(C<=0x30ff));
return (unsigned int) C >= 0x30a0u && (unsigned int) C <= 0x30ffu;
return((L'\u30a0'<=C)&&(C<=L'\u30ff'));

回答1:


It should be possible to cast (explicitly or implicitly) the character to an unsigned integer, and then just use such constants:

return (unsigned int) C >= 0x30a0u && (unsigned int) C <= 0x30ffu;

should do it.

By the way, I'd recommend against using a (single-character) uppercase argument name, it's very easy to think it's a compile-time constant (which are typically uppercase in C and C++).




回答2:


The error appears to be related to the use of the character literal, not the test. So test against the codepoint as an integral literal, eg:

bool isFullwidthKatakana(WideChar C)
{
  return(( (WideChar)0x30a0 <= C )&&(C <= (WideChar)0x30ff ));
}



回答3:


IIUC, you need to check if a wide unicode character (probably utf-16 since you're on Windows) is within a range. This can be done with the code you've shown, you just have to make the character literals wide character literals. In C++ and C, they are made by prepending L to the literal, eg. L'a' or L"ahoj".

In your case, I'd try

bool isFullwidthKatakana(WideChar C)
{
  return((L'\u30a0'<=C)&&(C<=L'\u30ff'));
}


来源:https://stackoverflow.com/questions/605985/how-to-check-if-a-unicode-character-is-within-given-range-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!