本文实例讲述了C++实现判断一个字符串是否为UTF8或GBK格式的方法。,具体如下:
在处理外部数据的时候,很可能因为数据格式不一样而导致乱码,甚至导致某些程序挂掉。鉴于对多数系统来说,使用是更被广泛使用的utf8,所以判断是不是utf8格式显得很重要了。
下面是一个判断字符串是否为utf8的函数:
bool is_str_utf8(const char* str)
{
unsigned int nBytes = 0;//UFT8可用1-6个字节编码,ASCII用一个字节
unsigned char chr = *str;
bool bAllAscii = true;
for (unsigned int i = 0; str[i] != '