一、判定字符串是否是UTF-8的编码

bool is_str_utf8(const char* str)
{
unsigned int nBytes = ;//UFT8可用1-6个字节编码,ASCII用一个字节
unsigned char chr = *str;
bool bAllAscii = true; for (unsigned int i = ; str[i] != '\0'; ++i)
{
chr = *(str + i);
//判断是否ASCII编码,如果不是,说明有可能是UTF8,ASCII用7位编码,最高位标记为0,0xxxxxxx
if (nBytes == && (chr & 0x80) != )
{
bAllAscii = false;
} if (nBytes == )
{
//如果不是ASCII码,应该是多字节符,计算字节数
if (chr >= 0x80)
{
if (chr >= 0xFC && chr <= 0xFD)
{
nBytes = ;
}
else if (chr >= 0xF8)
{
nBytes = ;
}
else if (chr >= 0xF0)
{
nBytes = ;
}
else if (chr >= 0xE0)
{
nBytes = ;
}
else if (chr >= 0xC0)
{
nBytes = ;
}
else
{
return false;
}
nBytes--;
}
}
else
{
//多字节符的非首字节,应为 10xxxxxx
if ((chr & 0xC0) != 0x80)
{
return false;
}
//减到为零为止
nBytes--;
}
} //违返UTF8编码规则
if (nBytes != )
{
return false;
} if (bAllAscii)
{ //如果全部都是ASCII, 也是UTF8
return true;
} return true;
}

二、判定字符串是否是GBk的编码

bool is_str_gbk(const char* str)
{
unsigned int nBytes = ;//GBK可用1-2个字节编码,中文两个 ,英文一个
unsigned char chr = *str;
bool bAllAscii = true; //如果全部都是ASCII, for (unsigned int i = ; str[i] != '\0'; ++i)
{
chr = *(str + i);
if ((chr & 0x80) != && nBytes == )
{// 判断是否ASCII编码,如果不是,说明有可能是GBK
bAllAscii = false;
} if (nBytes == )
{
if (chr >= 0x80)
{
if (chr >= 0x81 && chr <= 0xFE)
{
nBytes = +;
}
else
{
return false;
}
nBytes--;
}
}
else
{
if (chr < 0x40 || chr>0xFE)
{
return false;
}
nBytes--;
}//else end
} if (nBytes != )
{ //违返规则
return false;
} if (bAllAscii)
{ //如果全部都是ASCII, 也是GBK
return true;
} return true;
}

三、字符串由GBk编码转换成UTF-8编码

void ConvertGBKToUtf8(CString &strGBK)
{
int len=MultiByteToWideChar(CP_ACP, , (LPCTSTR)strGBK, -, NULL,);
wchar_t * wszUtf8 = new wchar_t [len];
memset(wszUtf8, , len);
MultiByteToWideChar(CP_ACP, , (LPCTSTR)strGBK, -, wszUtf8, len);
len = WideCharToMultiByte(CP_UTF8, , wszUtf8, -, NULL, , NULL, NULL);
char *szUtf8=new char[len + ];
memset(szUtf8, , len + );
WideCharToMultiByte (CP_UTF8, , wszUtf8, -, szUtf8, len, NULL,NULL);
strGBK = szUtf8;
delete[] szUtf8;
delete[] wszUtf8;
} string GBKToUTF8(const char* strGBK)
{
int len = MultiByteToWideChar(CP_ACP, , strGBK, -, NULL, );
wchar_t* wstr = new wchar_t[len+];
memset(wstr, , len+);
MultiByteToWideChar(CP_ACP, , strGBK, -, wstr, len);
len = WideCharToMultiByte(CP_UTF8, , wstr, -, NULL, , NULL, NULL);
char* str = new char[len+];
memset(str, , len+);
WideCharToMultiByte(CP_UTF8, , wstr, -, str, len, NULL, NULL);
string strTemp = str;
if(wstr) delete[] wstr;
if(str) delete[] str;
return strTemp;
}

四、字符串由UTF-8编码转换成GBk编码

string UtfToGbk(const char* utf8)
{
int len = MultiByteToWideChar(CP_UTF8, , utf8, -, NULL, );
wchar_t* wstr = new wchar_t[len+];
memset(wstr, , len+);
MultiByteToWideChar(CP_UTF8, , utf8, -, wstr, len);
len = WideCharToMultiByte(CP_ACP, , wstr, -, NULL, , NULL, NULL);
char* str = new char[len+];
memset(str, , len+);
WideCharToMultiByte(CP_ACP, , wstr, -, str, len, NULL, NULL);
if(wstr) delete[] wstr;
return str;
} bool Utf82gbk(std::string &gbkStr, std::string &srcStr)
{ //首先先将utf-8编码转换为unicode编码
if(NULL==setlocale(LC_ALL,"zh_CN.utf8"))//设置转换为unicode前的码,当前为utf8编码
{
printf("Bad Parameter\n");
return false;
} int unicodeLen=mbstowcs(NULL,srcStr.c_str(),);//计算转换后的长度
if(unicodeLen<=)
{
printf("Can not Transfer!!!\n");
return false;
}
wchar_t *unicodeStr=(wchar_t *)calloc(sizeof(wchar_t),unicodeLen+);
mbstowcs(unicodeStr,srcStr.c_str(),srcStr.size());//将gbk转换为unicode //将unicode编码转换为gbk编码
if(NULL==setlocale(LC_ALL,"zh_CN.gbk"))//设置unicode转换后的码,当前为gbk
{
printf("Bad Parameter\n");
return false;
}
int gbkLen = wcstombs(NULL,unicodeStr,);//计算转换后的长度
if(gbkLen<=)
{
printf("Can not Transfer!!!\n");
return false;
}
char gbkbuf[*];
wcstombs(gbkbuf,unicodeStr,gbkLen);
gbkbuf[gbkLen]=;//添加结束符
gbkStr = gbkbuf;
free(unicodeStr);
return true;
} string UTF8ToGBK(const std::string& strUTF8)
{
int len = MultiByteToWideChar(CP_UTF8, , strUTF8.c_str(), -, NULL, );
WCHAR* wszGBK = new WCHAR[len+];
memset(wszGBK, , len * + );
MultiByteToWideChar(CP_UTF8, , (LPCSTR)(LPCTSTR)strUTF8.c_str(), -, wszGBK, len); len = WideCharToMultiByte(CP_ACP, , wszGBK, -, NULL, , NULL, NULL);
char *szGBK = new char[len + ];
memset(szGBK, , len + );
WideCharToMultiByte(CP_ACP,, wszGBK, -, szGBK, len, NULL, NULL);
std::string strTemp(szGBK);
delete[]szGBK;
delete[]wszGBK;
return strTemp;
}

字符串UTF-8和GBK之间的转换以及判定的更多相关文章

  1. C++常用字符串操作和UTF-8和GBK之间的转换以及判定(转)

    编码转换原文地址:https://www.cnblogs.com/Toney-01-22/p/9935297.html C++字符串常用操作:C++ 中字符串查找.字符串截取.字符串替换

  2. UTF8,UTF16,UTF32,UTF16-LE,UTF16-BE,GBK 之间的转换

    Unicode是Unicode.org制定的编码标准,目前得到了绝大部分操作系统和编程语言的支持.Unicode.org官方对Unicode的定义是:Unicode provides a unique ...

  3. UTF8 & GBK之间的转换

    使用lua的时候,在lua中给字符串赋值的中文,但是在C中读出来的就是乱码,是因为在lua中使用的是UTF8编码,而在C(windows下面)中使用的是GBK编码,将UTF8转成GBK就可以了,下面的 ...

  4. [转]Json字符串和map和HashMap之间的转换

    需要导入alibaba.fastJsonmaven中的依赖为 <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson --> ...

  5. 【转】c#、wpf 字符串,color,brush之间的转换

    转自:http://www.cnblogs.com/wj-love/archive/2012/09/14/2685281.html 1,将#3C3C3C 赋给background this.selec ...

  6. angular2 ----字符串、对象、base64 之间的转换

    1. JSON对象转化为字符串 let obj = { "name":Ayinger; "sex":"女"; } let str = JSO ...

  7. 【C语言】字符串与整型数值之间的转换

    一.将字符串转化为对应的数值 /*============================================================================= # # F ...

  8. JavaScript中字符串与16进制之间的转换

    一.字符串转换为16进制 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> ...

  9. 常见的时间字符串与timestamp之间的转换 时间戳

    这里说的字符串不是一般意义上的字符串,是指在读取日期类型的数据时,如果还没有及时解析字符串,它就还不是日期类型,那么此时的字符串该怎么与时间戳之间进行转换呢? ① 时间字符串转化成时间戳 将时间字符串 ...

随机推荐

  1. Postman测试上传MultipartFile文件

    单个文件上传 后台代码 //导入excel @PostMapping("/import") public Result excelImport( @RequestParam(&qu ...

  2. linux 多并发 连接限制修改

    1. 修改 ulimit -a 查看 open files 表示单个用户能打开的最大句柄  如果开发的高并发当个进程打开的句柄需要很大. 修改/etc/security/limits.conf 里面有 ...

  3. SpringCloud(一)

    什么是SpringCloud? Spring Cloud是一系列框架的有序集合.它利用Spring Boot的开发便利性巧妙地简化了分布式系统基础设施的开发,如服务发现注册.配置中心.消息总线.负载均 ...

  4. 【内推】2020微软苏州Office365众多核心团队热招150+研发精英!欢迎推荐

    2020微软苏州Office365众多核心团队热招150+研发精英!欢迎推荐 大家好,目前微软Office365核心团队在美丽宜居的苏州有150多的社招职位虚位以待,欢迎大家自荐,推荐,转发!除以下列 ...

  5. 剑指offer-面试题9-用两个栈实现队列-栈和队列

    /* 题目: 用两个栈实现一个队列.队列声明如下. */ /* 思路: 将值压入stack1,再从stack1弹出到stack2,则为先进先出. appendTail时直接压入stack1即可,当st ...

  6. 静态路由、RIP、SOPF、VLAN间的路由

    常用命令: clear ip router * --清楚全部路由 show ip route --显示路由表 show ip inter b--显示接口信息 show ip protocols  -- ...

  7. 【HTML】html5 canvas全屏烟花动画特效

    <!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8&quo ...

  8. LayIM聊天框全屏根据浏览器高宽自适应

    个人博客 地址:http://www.wenhaofan.com/article/20190410190628 问题 由于LayIM没有处理聊天框在全屏状态下根据浏览器缩放处理高宽,所以会导致在浏览器 ...

  9. Spring学习笔记-Spring之旅-01

    使用Spring简化JAVA开发 Spring的四种关键策略: ●基于POJO的轻量级和最小侵入式编程: ●通过依赖注入(DI)和面向接口实现松耦合: ●基于切面(AOP)和惯例进行声明式编程. ●通 ...

  10. [Python]pyhon去除txt文件重复行 python 2020.2.10

    代码如下: import shutil readPath='E:/word4.txt' #要处理的文件 writePath='E:/word5.txt' #要写入的文件 lines_seen=set( ...