Posted on 2010-09-03 10:55
傅先生 阅读(508)
评论(0) 编辑 收藏 引用 所属分类:
数据-常用操作
// UTF_8.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <stdio.h>
#include <string.h>
#include <locale.h>
// 把UTF-8转换成Unicode
static void 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);
}
void UTF8_UNICODE(char *lpIn, int nLen, wchar_t *lpOut)
{
int i = 0;
int j = 0;
while( i < nLen )
{
wchar_t unicode = 0;
if( lpIn[i] > 0 )
{
*((char *)&unicode) = lpIn[i++];
}
else
{
UTF_8ToUnicode(&unicode, lpIn + i);
i += 3;
}
lpOut[j++] = unicode;
}
lpOut[j] = L'\0';
}
///////////------------ Unicode转UTF-8,字节
int cUxU8(char* pOut,wchar_t* pText){
int ret = 0;
unsigned char* pchar = (unsigned char *)pText;
if(pText[0]<=0x7f){ // ASCII 0x00 ~ 0x7f
pOut[0] = (char)pchar[0];
}else if(pText[0]<=0x7ff){ // 0x080 ~ 0x7ff
pOut[0] = 0xc0|(pchar[1]<<2)|(pchar[0]>>6);
pOut[1] = 0x80|(pchar[0]&0x3f);
ret = 1;
}else{ // 0x0800 ~ 0xFFFF
pOut[0] = 0xe0|(pchar[1]>>4);
pOut[1] = 0x80|((pchar[1]&0x0f)<<2)|(pchar[0]>>6);
pOut[2] = 0x80|(pchar[0]&0x3f);
ret = 2;
}
return ret;
}
// ------------ Unicode转UTF-8,字符串 ----------------
int sUxU8(char* pOut,wchar_t* pText,int Len){
int i,j;
for(i=0,j=0;i<Len;i++,j++){
j+=cUxU8(&pOut[j],&pText[i]);
}
return j;
}
int main(int argc, char* argv[])
{
wchar_t * p= L"111222ffdd你你你好你你你"; //测试代码
// --输出Unicode其编码
int plen = wcslen(p);
printf("开始unicode P :");
for(int i=0;i<plen;i++)//
{
printf("%X ",p[i]);
}
printf("\n"); printf("\n");
//开始从unicode~~~UTF-8 转换 --并输出其编码
char pOut[500];
sUxU8(pOut,p,plen*2);
printf("开始UTF-8 pOut :");
for(i=0;i<plen*3;i++)
{
printf("%X ",pOut[i]);
}
printf("\n"); printf("\n");
//开始从UTF-8~~~unicode 还原 --并输出其编码
wchar_t wtest[500];
UTF8_UNICODE(pOut, plen*2, wtest);
printf("开始Unicode 还原:");
for(i=0;i<plen*3;i++)
{
printf("%X ",wtest[i]);
}
printf("\n"); printf("\n");
///输出 Unicode字符
setlocale(LC_ALL,"chs");
wchar_t test[] = L"测试Test";
wprintf(L"内容还原 : %s \n",wtest);
printf("Hello World!\n");
return 0;
}