C++ Extended Ascii characters

2019-05-29 10:58发布

How to detect the presence of Extended ASCII values (128 to 255) in a C++ character array.

8条回答
叼着烟拽天下
2楼-- · 2019-05-29 11:21

Iterate over array and check that each character doesn't fall in 128 to 255 range?

查看更多
\"骚年 ilove
3楼-- · 2019-05-29 11:22
bool detect(const signed char* x) {
  while (*x++ > 0);
  return x[-1];
}
查看更多
Root(大扎)
4楼-- · 2019-05-29 11:24

Check the values that they are not negative

查看更多
叛逆
5楼-- · 2019-05-29 11:30

Make sure you know the endianness of the machine in question, and just check the highest bit with a bitwise AND mask:

if (ch & 128) {
  // high bit is set
} else {
  // looks like a 7-bit value
}

But there are probably locale functions you should be using for this. Better yet, KNOW what character encoding data is coming in as. Trying to guess it is like trying to guess the format of data going into your database fields. It might go in, but garbage in, garbage out.

查看更多
Fickle 薄情
6楼-- · 2019-05-29 11:37
(char) c = (char) 200;

if (isascii(c))
{
    cout << "it's ascii!" << endl;
}
else
{
    cout << "it's not ascii!" << endl;
}

try this code

查看更多
在下西门庆
7楼-- · 2019-05-29 11:39

Doesn't anyone use isascii anymore?

char c = (char) 200;

if (isascii(c))
{
    cout << "it's ascii!" << endl;
}
else
{
    cout << "it's not ascii!" << endl;
}
查看更多
登录 后发表回答