// c:\Program Files\Microsoft SDKs\Windows\v7.0A\Include\WinNls.h
#define CP_ACP 0 // default to ANSI code page
#define CP_OEMCP 1 // default to OEM code page
#define CP_MACCP 2 // default to MAC code page
#define CP_THREAD_ACP 3 // current thread's ANSI code page
#define CP_SYMBOL 42 // SYMBOL translations #define CP_UTF7 65000 // UTF-7 translation
#define CP_UTF8 65001 // UTF-8 translation // -- c:\Program Files\Microsoft SDKs\Windows\v7.0A\Include\WinNT.h
//
// UNICODE (Wide Character) types
//
#ifndef _MAC
typedef wchar_t WCHAR; // wc, 16-bit UNICODE character
#else
// some Macintosh compilers don't define wchar_t in a convenient location, or define it as a char
typedef unsigned short WCHAR; // wc, 16-bit UNICODE character
#endif typedef WCHAR *PWCHAR, *LPWCH, *PWCH;
typedef CONST WCHAR *LPCWCH, *PCWCH; typedef __nullterminated WCHAR *NWPSTR, *LPWSTR, *PWSTR;
......
typedef __nullterminated CONST WCHAR *LPCWSTR, *PCWSTR;
...... //
// ANSI (Multi-byte Character) types
//
typedef CHAR *PCHAR, *LPCH, *PCH;
typedef CONST CHAR *LPCCH, *PCCH; typedef __nullterminated CHAR *NPSTR, *LPSTR, *PSTR;
......
typedef __nullterminated CONST CHAR *LPCSTR, *PCSTR;
...... //
// 多字节(一般指GBK) utf8 Unicode 编码互转
/*
MultiByteToWideChar: 将MultiByte(多字节编码(CP_ACP)、GBK(CP_ACP)、UTF8(CP_UTF8))转换为WideChar(Unicode 编码)。
如 MultiByteToWideChar(CP_ACP, 0, old_str, old_str_len, new_str, new_Len);
表示将CP_ACP类型的old_str转换为WideChar类型;CP_ACP标识 从哪种类型的MultiByte转换为WideChar。
WideCharToMultiByte: 将WideChar(Unicode 编码)转换为MultiByte(多字节编码(CP_ACP)、GBK(CP_ACP)、UTF8(CP_UTF8))。
如 WideCharToMultiByte(CP_UTF8, 0, old_str, old_str_len, new_str, new_Len, NULL, NULL);
表示将WideChar类型的old_str转换为CP_UTF8类型的MultiByte。CP_ACP标识 WideChar要转换为哪种类型的MultiByte。
MultiByte1 转换为 MultiByte2 ,先将MultiByte1转为将WideChar,再从将WideChar转为MultiByte2。
*/ std::string Utf8ToGBK(const char* strUtf8)
{
//不是utf8编码,返回原串
unsigned char code_mark = strUtf8[];
if(code_mark <= 0xE0)
{
return strUtf8;
} int len=MultiByteToWideChar(CP_UTF8, , (LPCSTR)strUtf8, -, NULL,);
unsigned short * wszGBK = new unsigned short[len+];
memset(wszGBK, , len * + );
MultiByteToWideChar(CP_UTF8, , (LPCSTR)strUtf8, -, (LPWSTR)wszGBK, len);
len = WideCharToMultiByte(CP_ACP, , (LPCWSTR)wszGBK, -, NULL, , NULL, NULL);
char *szGBK=new char[len + ];
memset(szGBK, , len + );
WideCharToMultiByte (CP_ACP, , (LPCWSTR)wszGBK, -, (LPSTR)szGBK, len, NULL,NULL);
std::string gbkString = szGBK;
delete[] wszGBK;
delete[] szGBK;
return gbkString;
} // 多字节编码转为UTF8编码
bool MBToUTF8(vector<char>& pu8, const char* pmb, int32 mLen)
{
// convert an MBCS string to widechar
int32 nLen = MultiByteToWideChar(CP_ACP, , pmb, mLen, NULL, ); WCHAR* lpszW = NULL;
try
{
lpszW = new WCHAR[nLen];
}
catch(bad_alloc &memExp)
{
return false;
} int32 nRtn = MultiByteToWideChar(CP_ACP, , pmb, mLen, lpszW, nLen); if(nRtn != nLen)
{
delete[] lpszW;
return false;
}
// convert an widechar string to utf8
int32 utf8Len = WideCharToMultiByte(CP_UTF8, , lpszW, nLen, NULL, , NULL, NULL);
if (utf8Len <= )
{
return false;
}
pu8.resize(utf8Len);
nRtn = WideCharToMultiByte(CP_UTF8, , lpszW, nLen, &*pu8.begin(), utf8Len, NULL, NULL);
delete[] lpszW; if (nRtn != utf8Len)
{
pu8.clear();
return false;
}
return true;
} // UTF8编码转为多字节编码
bool UTF8ToMB(vector<char>& pmb, const char* pu8, int32 utf8Len)
{
// convert an UTF8 string to widechar
int32 nLen = MultiByteToWideChar(CP_UTF8, , pu8, utf8Len, NULL, ); WCHAR* lpszW = NULL;
try
{
lpszW = new WCHAR[nLen];
}
catch(bad_alloc &memExp)
{
return false;
} int32 nRtn = MultiByteToWideChar(CP_UTF8, , pu8, utf8Len, lpszW, nLen); if(nRtn != nLen)
{
delete[] lpszW;
return false;
} // convert an widechar string to Multibyte
int32 MBLen = WideCharToMultiByte(CP_ACP, , lpszW, nLen, NULL, , NULL, NULL);
if (MBLen <=)
{
return false;
}
pmb.resize(MBLen);
nRtn = WideCharToMultiByte(CP_ACP, , lpszW, nLen, &*pmb.begin(), MBLen, NULL, NULL);
delete[] lpszW; if(nRtn != MBLen)
{
pmb.clear();
return false;
}
return true;
} // 多字节编码转为Unicode编码
bool MBToUnicode(vector<wchar_t>& pun, const char* pmb, int32 mLen)
{
// convert an MBCS string to widechar
int32 uLen = MultiByteToWideChar(CP_ACP, , pmb, mLen, NULL, ); if (uLen<=)
{
return false;
}
pun.resize(uLen); int32 nRtn = MultiByteToWideChar(CP_ACP, , pmb, mLen, &*pun.begin(), uLen); if (nRtn != uLen)
{
pun.clear();
return false;
}
return true;
} //Unicode编码转为多字节编码
bool UnicodeToMB(vector<char>& pmb, const wchar_t* pun, int32 uLen)
{
// convert an widechar string to Multibyte
int32 MBLen = WideCharToMultiByte(CP_ACP, , pun, uLen, NULL, , NULL, NULL);
if (MBLen <=)
{
return false;
}
pmb.resize(MBLen);
int nRtn = WideCharToMultiByte(CP_ACP, , pun, uLen, &*pmb.begin(), MBLen, NULL, NULL); if(nRtn != MBLen)
{
pmb.clear();
return false;
}
return true;
} // UTF8编码转为Unicode
bool UTF8ToUnicode(vector<wchar_t>& pun, const char* pu8, int32 utf8Len)
{
// convert an UTF8 string to widechar
int32 nLen = MultiByteToWideChar(CP_UTF8, , pu8, utf8Len, NULL, );
if (nLen <=)
{
return false;
}
pun.resize(nLen);
int32 nRtn = MultiByteToWideChar(CP_UTF8, , pu8, utf8Len, &*pun.begin(), nLen); if(nRtn != nLen)
{
pun.clear();
return false;
} return true;
} // Unicode编码转为UTF8
bool UnicodeToUTF8(vector<char>& pu8, const wchar_t* pun, int32 uLen)
{
// convert an widechar string to utf8
int32 utf8Len = WideCharToMultiByte(CP_UTF8, , pun, uLen, NULL, , NULL, NULL);
if (utf8Len<=)
{
return false;
}
pu8.resize(utf8Len);
int32 nRtn = WideCharToMultiByte(CP_UTF8, , pun, uLen, &*pu8.begin(), utf8Len, NULL, NULL); if (nRtn != utf8Len)
{
pu8.clear();
return false;
}
return true;
}

多字节(一般指GBK) utf8 Unicode 编码互转的更多相关文章

  1. utf8 unicode 编码互转

    static function utf8_to_unicode($c) { switch(strlen($c)) { case 1: return ord($c); case 2: $n = (ord ...

  2. MySQL 解决 emoji表情 的方法,使用utf8mb4 字符集(4字节 UTF-8 Unicode 编码)

    p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px 'Helvetica Neue'; color: #454545} span.s1 {font: ...

  3. GBK/ UTF-8/ UNICODE(字符编码)

    在python2中:如果执行程序,在编译器中,因为默认的编码是ASCII码(英文),所以如果输入中文就会出现乱码,因此为了避免这种乱码的情况发生,在输入中文字符串之后,必须进行手动转码,将GBK/ U ...

  4. C# unicode GBK UTF-8和汉字互转

    界面: 源码: using System; using System.Collections.Generic; using System.ComponentModel; using System.Da ...

  5. emoji表情与unicode编码互转(JS,JAVA,C#)

    1.表情字符转编码 [C#] Encoding.UTF32.GetBytes("

  6. 字符编码-UNICODE,GBK,UTF-8区别【转转】

    字符编码介绍及不同编码区别 今天看到这篇关于字符编码的文章,抑制不住喜悦(总结的好详细)所以转到这里来.转自:祥龙之子http://www.cnblogs.com/cy163/archive/2007 ...

  7. 【JAVA编码专题】UNICODE,GBK,UTF-8区别

    简单来说,unicode,gbk和大五码就是编码的值,而utf-8,uft-16之类就是这个值的表现形式.而前面那三种编码是一兼容的,同一个汉字,那三个码值是完全不一样的.如"汉"的uncode值与g ...

  8. BIG5, GB(GB2312, GBK, ...), Unicode编码, UTF8, WideChar, MultiByte, Char说明与区别

    汉语unicode编译方式,BIG5是繁体规范,GB是简体规范 GB是大陆使用的国标码,BIG5码,又叫大五码,是台湾使用的繁体码. BIG5编码, GB编码(GB2312, GBK, ...), U ...

  9. 一文读懂所有的编码方式(UTF-8、GBK、Unicode、宽字节...)

    编码方式就分两类:ANSI编码.Unicode编码.这两类编码都兼容ASC码. ------------------------------------------------------------ ...

随机推荐

  1. dp之混合背包poj1742(推荐)

    题意:给你价值为a1,a2.....的货币,每种有c1,c2.......个,求这些货币所能组成的价值小于等于m有多少个..... 思路:很像一道多重背包题?那我一开始的确是用多重背包的思路编写的.. ...

  2. jsp报源码

    刚在get的一个姿势.在参数后面加负号即爆出源码. w7oami 表哥解释道其原理如下: 1.用了@file_get_contents 函数 2.cdn 或者负载均衡 才导致爆出源码.

  3. kafka的分区模式?

    当别人问这个问题的时候,别人肯定是想你是否看过源码.是否针对不同场景改过kafka的分区模式 这是别人最想知道的是,你的message如何负载均衡的发送给topic的partition 我们用kafk ...

  4. Quartz 与 Spring集成

    http://www.cnblogs.com/pigwing/archive/2011/07/12/2104002.html http://blog.arganzheng.me/posts/quart ...

  5. 【大数据笔记】白话详解Zookeeper的一致性

    下面内容主要摘抄于<<Hadoop实战>>,红色高亮部分是本人添加的白话注释. Zookeeper 是一种高性能.可扩展的服务. Zookeeper 的读写速度非常快,并且读的 ...

  6. drupal7请求异常,执行时间过长的解决方法

    drupal7请求错误,执行时间过长的解决办法 根据你的系统或网络设置Drupal不能读取网页,造成功能缺失.可能是web服务器配置或PHP设置引起的,可用更新.获取更新源.使用OpenID登 录或使 ...

  7. 单网卡绑定多个ip, 多个网卡绑定成一块虚拟网卡

    Linux网卡配置与绑定   Redhat Linux的网络配置,基本上是通过修改几个配置文件来实现的,虽然也可以用ifconfig来设置IP,用route来配置默认网关,用hostname来配置主机 ...

  8. FastDFS - 文件服务器学习资料

    FastDFS搭建及java整合代码 CentOS 6.5下 FastDFS结合Nginx插件实现图片http访问 图片服务器fastDFS的搭建以及配置 nginx fastdfs 配置后 上传成功 ...

  9. LVS学习笔记及总结(思维导图版)

    转自: http://www.07net01.com/2015/10/944377.html 下图是我在跟随马哥的脚步学习LVS过程中的学习笔记,以此为蓝本总结的,若有不足之处请谅解!

  10. python中时间操作总结

    一.time 二.datetime 1.获取当前系统时间 datenow = datetime.datetime.now() 2.将datetime格式的时间转换成str datenow = date ...