Unicode 与 Utf-8 应用实例

Posted on 2010-09-03 10:55 傅先生 阅读(505) 评论(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_tpOut,char *pText)
{
    
charuchar = (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 *lpInint nLenwchar_t *lpOut)
{
    
int 0;
    
int 0;
    
whilenLen )
    {
        
wchar_t unicode 0;
        
iflpIn[i] > )
        {
            *((
char *)&unicode) = lpIn[i++];                       
        }
        
else                 
        
{
            
UTF_8ToUnicode(&unicodelpIn i);
            
+= 3;    
        }
        
lpOut[j++] = unicode;
    }
    
lpOut[j] = L'\0';
}

///////////------------ Unicode转UTF-8,字节 
int cUxU8(charpOut,wchar_tpText){ 
    
int ret 0
    
unsigned charpchar = (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(charpOut,wchar_tpText,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 argccharargv[])
{
    
    
wchar_t pL"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(pOutplen*2wtest);
    
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;
}










只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


posts - 54, comments - 5, trackbacks - 0, articles - 2

Copyright © 傅先生