字符集之间转换(UTF-8,UNICODE,Gb2312)c++and java
2012-03-28 18:02
627 查看
UTF-8: 3字节一个字符
UNICODE: 2字节一个字符
GB2312: 1字节一个字符
例子:
“你”字的UTF-8编码: E4 BD A0 11100100 10111101 10100000
“你”的Unicode编码: 4F 60 01001111 01100000
按照UTF-8的编码规则,分解如下:xxxx0100 xx111101 xx100000
把除了x之外的数字拼接在一起,就变成“你”的Unicode编码了。
注意UTF-8的最前面3个1,表示整个UTF-8串是由3个字节构成的。
经过UTF-8编码之后,再也不会出现敏感字符了,因为最高位始终为1。
一 windows c++
类定义
class CChineseCode
{
public:
static void UTF_8ToUnicode(wchar_t* pOut,char *pText); // 把UTF-8转换成Unicode
static void UnicodeToUTF_8(char* pOut,wchar_t* pText); //Unicode 转换成UTF-8
static void UnicodeToGB2312(char* pOut,wchar_t uData); // 把Unicode 转换成 GB2312
static void Gb2312ToUnicode(wchar_t* pOut,char *gbBuffer);// GB2312 转换成 Unicode
static void GB2312ToUTF_8(string& pOut,char *pText, int pLen);//GB2312 转为 UTF-8
static void UTF_8ToGB2312(string &pOut, char *pText, int pLen);//UTF-8 转为 GB2312
};
类实现
void CChineseCode::UTF_8ToUnicode(wchar_t* pOut,char *pText)
{
char* uchar = (char *)pOut;
uchar[1] = ((pText[0] & 0x0F) << 4) + ((pText[1] >> 2) & 0x0F);
uchar[0] = ((pText[1] & 0x03) << 6) + (pText[2] & 0x3F);
return;
}
void CChineseCode::UnicodeToUTF_8(char* pOut,wchar_t* pText)
{
// 注意 WCHAR高低字的顺序,低字节在前,高字节在后
char* pchar = (char *)pText;
pOut[0] = (0xE0 | ((pchar[1] & 0xF0) >> 4));
pOut[1] = (0x80 | ((pchar[1] & 0x0F) << 2)) + ((pchar[0] & 0xC0) >> 6);
pOut[2] = (0x80 | (pchar[0] & 0x3F));
return;
}
void CChineseCode::UnicodeToGB2312(char* pOut,wchar_t uData)
{
WideCharToMultiByte(CP_ACP,NULL,&uData,1,pOut,sizeof(wchar_t),NULL,NULL);
return;
}
void CChineseCode::Gb2312ToUnicode(wchar_t* pOut,char *gbBuffer)
{
::MultiByteToWideChar(CP_ACP,MB_PRECOMPOSED,gbBuffer,2,pOut,1);
return ;
}
void CChineseCode::GB2312ToUTF_8(string& pOut,char *pText, int pLen)
{
char buf[4];
int nLength = pLen* 3;
char* rst = new char[nLength];
memset(buf,0,4);
memset(rst,0,nLength);
int i = 0;
int j = 0;
while(i < pLen)
{
//如果是英文直接复制就能
if( *(pText + i) >= 0)
{
rst[j++] = pText[i++];
}
else
{
wchar_t pbuffer;
Gb2312ToUnicode(&pbuffer,pText+i);
UnicodeToUTF_8(buf,&pbuffer);
unsigned short int tmp = 0;
tmp = rst[j] = buf[0];
tmp = rst[j+1] = buf[1];
tmp = rst[j+2] = buf[2];
j += 3;
i += 2;
}
}
rst[j] = ’’;
//返回结果
pOut = rst;
delete []rst;
return;
}
void CChineseCode::UTF_8ToGB2312(string &pOut, char *pText, int pLen)
{
char * newBuf = new char[pLen];
char Ctemp[4];
memset(Ctemp,0,4);
int i =0;
int j = 0;
while(i < pLen)
{
if(pText > 0)
{
newBuf[j++] = pText[i++];
}
else
{
WCHAR Wtemp;
UTF_8ToUnicode(&Wtemp,pText + i);
UnicodeToGB2312(Ctemp,Wtemp);
newBuf[j] = Ctemp[0];
newBuf[j + 1] = Ctemp[1];
i += 3;
j += 2;
}
}
newBuf[j] = ’’;
pOut = newBuf;
delete []newBuf;
return;
}
二 linux c++
UNICODE: 2字节一个字符
GB2312: 1字节一个字符
例子:
“你”字的UTF-8编码: E4 BD A0 11100100 10111101 10100000
“你”的Unicode编码: 4F 60 01001111 01100000
按照UTF-8的编码规则,分解如下:xxxx0100 xx111101 xx100000
把除了x之外的数字拼接在一起,就变成“你”的Unicode编码了。
注意UTF-8的最前面3个1,表示整个UTF-8串是由3个字节构成的。
经过UTF-8编码之后,再也不会出现敏感字符了,因为最高位始终为1。
一 windows c++
类定义
class CChineseCode
{
public:
static void UTF_8ToUnicode(wchar_t* pOut,char *pText); // 把UTF-8转换成Unicode
static void UnicodeToUTF_8(char* pOut,wchar_t* pText); //Unicode 转换成UTF-8
static void UnicodeToGB2312(char* pOut,wchar_t uData); // 把Unicode 转换成 GB2312
static void Gb2312ToUnicode(wchar_t* pOut,char *gbBuffer);// GB2312 转换成 Unicode
static void GB2312ToUTF_8(string& pOut,char *pText, int pLen);//GB2312 转为 UTF-8
static void UTF_8ToGB2312(string &pOut, char *pText, int pLen);//UTF-8 转为 GB2312
};
类实现
void CChineseCode::UTF_8ToUnicode(wchar_t* pOut,char *pText)
{
char* uchar = (char *)pOut;
uchar[1] = ((pText[0] & 0x0F) << 4) + ((pText[1] >> 2) & 0x0F);
uchar[0] = ((pText[1] & 0x03) << 6) + (pText[2] & 0x3F);
return;
}
void CChineseCode::UnicodeToUTF_8(char* pOut,wchar_t* pText)
{
// 注意 WCHAR高低字的顺序,低字节在前,高字节在后
char* pchar = (char *)pText;
pOut[0] = (0xE0 | ((pchar[1] & 0xF0) >> 4));
pOut[1] = (0x80 | ((pchar[1] & 0x0F) << 2)) + ((pchar[0] & 0xC0) >> 6);
pOut[2] = (0x80 | (pchar[0] & 0x3F));
return;
}
void CChineseCode::UnicodeToGB2312(char* pOut,wchar_t uData)
{
WideCharToMultiByte(CP_ACP,NULL,&uData,1,pOut,sizeof(wchar_t),NULL,NULL);
return;
}
void CChineseCode::Gb2312ToUnicode(wchar_t* pOut,char *gbBuffer)
{
::MultiByteToWideChar(CP_ACP,MB_PRECOMPOSED,gbBuffer,2,pOut,1);
return ;
}
void CChineseCode::GB2312ToUTF_8(string& pOut,char *pText, int pLen)
{
char buf[4];
int nLength = pLen* 3;
char* rst = new char[nLength];
memset(buf,0,4);
memset(rst,0,nLength);
int i = 0;
int j = 0;
while(i < pLen)
{
//如果是英文直接复制就能
if( *(pText + i) >= 0)
{
rst[j++] = pText[i++];
}
else
{
wchar_t pbuffer;
Gb2312ToUnicode(&pbuffer,pText+i);
UnicodeToUTF_8(buf,&pbuffer);
unsigned short int tmp = 0;
tmp = rst[j] = buf[0];
tmp = rst[j+1] = buf[1];
tmp = rst[j+2] = buf[2];
j += 3;
i += 2;
}
}
rst[j] = ’’;
//返回结果
pOut = rst;
delete []rst;
return;
}
void CChineseCode::UTF_8ToGB2312(string &pOut, char *pText, int pLen)
{
char * newBuf = new char[pLen];
char Ctemp[4];
memset(Ctemp,0,4);
int i =0;
int j = 0;
while(i < pLen)
{
if(pText > 0)
{
newBuf[j++] = pText[i++];
}
else
{
WCHAR Wtemp;
UTF_8ToUnicode(&Wtemp,pText + i);
UnicodeToGB2312(Ctemp,Wtemp);
newBuf[j] = Ctemp[0];
newBuf[j + 1] = Ctemp[1];
i += 3;
j += 2;
}
}
newBuf[j] = ’’;
pOut = newBuf;
delete []newBuf;
return;
}
二 linux c++
在LINUX上进行编码转换时,既可以利用iconv函数族编程实现,也可以利用iconv命令来实现,只不过后者是针对文件的,即将指定文件从一种编码转换为另一种编码。 2.1 利用iconv函数族进行编码转换 iconv函数族的头文件是iconv.h,使用前需包含之。 #include <iconv.h> iconv函数族有三个函数,原型如下: (1) iconv_t iconv_open(const char *tocode, const char *fromcode); 此函数说明将要进行哪两种编码的转换,tocode是目标编码,fromcode是原编码,该函数返回一个转换句柄,供以下两个函数使用。 (2) size_t iconv(iconv_t cd,char **inbuf,size_t *inbytesleft,char **outbuf,size_t *outbytesleft); 此函数从inbuf中读取字符,转换后输出到outbuf中,inbytesleft用以记录还未转换的字符数,outbytesleft用以记录输出缓冲的剩余空间。 (3) int iconv_close(iconv_t cd); 此函数用于关闭转换句柄,释放资源。 例子1: 用C语言实现的转换示例程序 /* f.c : 代码转换示例C程序 */ #include <iconv.h> #define OUTLEN 255 main() { char *in_utf8 = "姝e?ㄥ??瑁?"; char *in_gb2312 = "正在安装"; char out[OUTLEN]; //unicode码转为gb2312码 rc = u2g(in_utf8,strlen(in_utf8),out,OUTLEN); printf("unicode-->gb2312 out=%sn",out); //gb2312码转为unicode码 rc = g2u(in_gb2312,strlen(in_gb2312),out,OUTLEN); printf("gb2312-->unicode out=%sn",out); } //代码转换:从一种编码转为另一种编码 int code_convert(char *from_charset,char *to_charset,char *inbuf,int inlen,char *outbuf,int outlen) { iconv_t cd; int rc; char **pin = &inbuf; char **pout = &outbuf; cd = iconv_open(to_charset,from_charset); if (cd==0) return -1; memset(outbuf,0,outlen); if (iconv(cd,pin,&inlen,pout,&outlen)==-1) return -1; iconv_close(cd); return 0; } //UNICODE码转为GB2312码 int u2g(char *inbuf,int inlen,char *outbuf,int outlen) { return code_convert("utf-8","gb2312",inbuf,inlen,outbuf,outlen); } //GB2312码转为UNICODE码 int g2u(char *inbuf,size_t inlen,char *outbuf,size_t outlen) { return code_convert("gb2312","utf-8",inbuf,inlen,outbuf,outlen); } 例子2: 用C++语言实现的转换示例程序 /* f.cpp : 代码转换示例C++程序 */ #include <iconv.h> #include <iostream> #define OUTLEN 255 using namespace std; // 代码转换操作类 class CodeConverter { private: iconv_t cd; public: // 构造 CodeConverter(const char *from_charset,const char *to_charset) { cd = iconv_open(to_charset,from_charset); } // 析构 ~CodeConverter() { iconv_close(cd); } // 转换输出 int convert(char *inbuf,int inlen,char *outbuf,int outlen) { char **pin = &inbuf; char **pout = &outbuf; memset(outbuf,0,outlen); return iconv(cd,pin,(size_t *)&inlen,pout,(size_t *)&outlen); } }; int main(int argc, char **argv) { char *in_utf8 = "姝e?ㄥ??瑁?"; char *in_gb2312 = "正在安装"; char out[OUTLEN]; // utf-8-->gb2312 CodeConverter cc = CodeConverter("utf-8","gb2312"); cc.convert(in_utf8,strlen(in_utf8),out,OUTLEN); cout << "utf-8-->gb2312 in=" << in_utf8 << ",out=" << out << endl; // gb2312-->utf-8 CodeConverter cc2 = CodeConverter("gb2312","utf-8"); cc2.convert(in_gb2312,strlen(in_gb2312),out,OUTLEN); cout << "gb2312-->utf-8 in=" << in_gb2312 << ",out=" << out << endl; } linux C 字符集转换,UTF-8,GB2312 最近帮朋友写个系统接口的小东东,2个系统字符集不同,一个采用UTF-8,一个采用GB2312,不得已需要转换字符集。转换函数记录如下: #include <iconv.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #define OUTLEN 255 main() { char *in_utf8 = "utf8字符串"; char *in_gb2312 = "\xbe\xb2\xcc\xac\xc4\xa3\xca\xbd"; char out[OUTLEN]; int rec ; //unicode码转为gb2312码 rec = u2g(in_utf8,strlen(in_utf8),out,OUTLEN); printf("unicode-->gb2312 out=%s\n",out); //gb2312码转为unicode码 rec = g2u(in_gb2312,strlen(in_gb2312),out,OUTLEN); printf("gb2312-->unicode out=%s \n",out); } //代码转换:从一种编码转为另一种编码 int code_convert(char *from_charset,char *to_charset,char *inbuf,int inlen,char *outbuf,int outlen) { iconv_t cd; int rc; char **pin = &inbuf; char **pout = &outbuf; cd = iconv_open(to_charset,from_charset); if (cd==0) return -1; memset(outbuf,0,outlen); if (iconv(cd,pin,&inlen,pout,&outlen)==-1) return -1; iconv_close(cd); return 0; } //UNICODE码转为GB2312码 int u2g(char *inbuf,int inlen,char *outbuf,int outlen) { return code_convert("utf-8","gb2312",inbuf,inlen,outbuf,outlen); } //GB2312码转为UNICODE码 int g2u(char *inbuf,size_t inlen,char *outbuf,size_t outlen) { return code_convert("gb2312","utf-8",inbuf,inlen,outbuf,outlen); } 三 java 语言 实现思路大致如下: 取得一个汉字的Unicode码 把Unicode码分解为两个16进制数据字符串(丢弃前两个字节) 把这两个16进制数据字符串转换成二进制数据字符串 把二进制数据字符串分解为三个串,第一个串为4(0~4)个位,在高位加上标记位“1110”,第二(4~10)、三个(10~16)串均为6个位,分别在高位加上“10”标记位 把这三个二进制串分别转换为10进制数据并赋值给字节型数组 根据这个字节型数组构造UTF-8字符 import java.io.File; import java.io.FileOutputStream; import java.io.UnsupportedEncodingException; public class CharsetConvertor { public static void main(String[] args) { String str = "This is a test for *中网!@#$。,?"; try { File f = new File("D:/test.txt"); FileOutputStream fio = new FileOutputStream(f); String s = gbToUtf8(str); fio.write(s.getBytes("UTF-8")); fio.close(); } catch (Exception e) { e.printStackTrace(); } } public static String gbToUtf8(String str) throws UnsupportedEncodingException { StringBuffer sb = new StringBuffer(); for (int i = 0; i < str.length(); i++) { String s = str.substring(i, i + 1); if (s.charAt(0) > 0x80) { byte[] bytes = s.getBytes("Unicode"); String binaryStr = ""; for (int j = 2; j < bytes.length; j += 2) { // the first byte String hexStr = getHexString(bytes[j + 1]); String binStr = getBinaryString(Integer.valueOf(hexStr, 16)); binaryStr += binStr; // the second byte hexStr = getHexString(bytes[j]); binStr = getBinaryString(Integer.valueOf(hexStr, 16)); binaryStr += binStr; } // convert unicode to utf-8 String s1 = "1110" + binaryStr.substring(0, 4); String s2 = "10" + binaryStr.substring(4, 10); String s3 = "10" + binaryStr.substring(10, 16); byte[] bs = new byte[3]; bs[0] = Integer.valueOf(s1, 2).byteValue(); bs[1] = Integer.valueOf(s2, 2).byteValue(); bs[2] = Integer.valueOf(s3, 2).byteValue(); String ss = new String(bs, "UTF-8"); sb.append(ss); } else { sb.append(s); } } return sb.toString(); } private static String getHexString(byte b) { String hexStr = Integer.toHexString(b); int m = hexStr.length(); if (m < 2) { hexStr = "0" + hexStr; } else { hexStr = hexStr.substring(m - 2); } return hexStr; } private static String getBinaryString(int i) { String binaryStr = Integer.toBinaryString(i); int length = binaryStr.length(); for (int l = 0; l < 8 - length; l++) { binaryStr = "0" + binaryStr; } return binaryStr; } } |
相关文章推荐
- 字符集之间转换(UTF-8,UNICODE,Gb2312) c++
- 字符集之间转换(UTF-8,UNICODE,Gb2312) c++
- c++字符集之间转换(UTF-8,UNICODE,Gb2312)
- c++字符集之间转换(UTF-8,UNICODE,Gb2312)
- c++字符集之间转换(UTF-8,UNICODE,Gb2312)
- 字符集之间转换(UTF-8,UNICODE,Gb2312) c++
- c++字符集之间转换(UTF-8,UNICODE,Gb2312)
- c++字符集之间转换(UTF-8,UNICODE,Gb2312)
- 字符集之间转换(UTF-8,UNICODE,Gb2312)
- 字符集之间转换(UTF-8,UNICODE,Gb2312)
- 字符集之间转换(UTF-8,UNICODE,Gb2312)
- 字符集之间转换(UTF-8,UNICODE,Gb2312)
- 字符集之间转换(UTF-8,UNICODE,Gb2312)
- 字符集之间转换(UTF-8,UNICODE,Gb2312)
- 字符集之间转换(UTF-8,UNICODE,Gb2312)
- 字符集之间转换(UTF-8,UNICODE,Gb2312)
- 字符集编码之间的转换,UTF-8 转为 GB2312,GB2312 转为 UTF-8 ,Unicode 转换成UTF-8
- 字符集之间转换(UTF-8,UNICODE,Gb2312)
- 字符集之间转换(UTF-8,UNICODE,Gb2312)
- 字符集之间转换(UTF-8,UNICODE,Gb2312)