随笔 - 96  文章 - 255  trackbacks - 0
<2008年4月>
303112345
6789101112
13141516171819
20212223242526
27282930123
45678910

E-mail:zbln426@163.com QQ:85132383 长期寻找对战略游戏感兴趣的合作伙伴。

常用链接

留言簿(21)

随笔分类

随笔档案

SDL相关网站

我的个人网页

我的小游戏

资源下载

搜索

  •  

积分与排名

  • 积分 - 485062
  • 排名 - 37

最新评论

阅读排行榜

评论排行榜

Windows only. Linux needn't.

//UVi Soft ( 2008 )
//Long Fei ( lf426 ), E-mail: zbln426@163.com
//Laboratory of ZaiBieLiunNian
//http://www.cppblog.com/lf426/

//FileName: gb2312_to_Unicode.h
//For Windows only


#ifndef GB2312_TO_UNICODE_H_
#define GB2312_TO_UNICODE_H_

#include 
<iostream>
#include 
<vector>
#include 
"GNU/iconv.h"

//if not include "SDL/SDL.h"
#ifndef _SDL_H
typedef unsigned 
short int Uint16;
#endif

bool getUnicode(const std::string& str, std::vector<Uint16>& unicodeVectorArray);

#endif

//UVi Soft ( 2008 )
//Long Fei ( lf426 ), E-mail: zbln426@163.com
//Laboratory of ZaiBieLiunNian
//http://www.cppblog.com/lf426/

//FileName: gb2312_to_Unicode.cpp
//For Windows only

#include 
"gb2312_to_Unicode.h"

int myUTF8_to_UNICODE(Uint16* unicode, unsigned char* utf8, int len);

bool getUnicode(const std::string& str, std::vector<Uint16>& unicodeVectorArray)
{
    
const int CHAR_SIZE = 256;
    
//GB2312 src
    const unsigned char* src = (const unsigned char*)(str.c_str());
    size_t src_len 
= strlen((char*)src);
    
//Unicode dst to get
    unsigned char dst[CHAR_SIZE] = {0};
    size_t dst_len 
= sizeof(dst);
    
//iconv arg
    const unsigned char* in = src;
    unsigned 
char* out = dst;

    iconv_t cd;
    
//GB2312 to UTF-8
    cd = iconv_open("UTF-8""GB2312");
    
if ((iconv_t)-1 == cd){
        
return false;
    }
    
//conversion
    iconv(cd, (const char**)&in&src_len, (char**)&out&dst_len);

    
//UTF-8 to Unicode
    int utf8Len = strlen((char*)dst);
    Uint16 unicodeData[CHAR_SIZE] 
= {0};
    
int unicodeLen = myUTF8_to_UNICODE(unicodeData, dst, utf8Len);
    
for (int i = 0; i < unicodeLen; i++) {
        unicodeVectorArray.push_back(unicodeData[i]);
    }
    
    iconv_close(cd); 
    
return true;
}

int myUTF8_to_UNICODE(Uint16* unicode, unsigned char* utf8, int len)
{
    
int length;
    unsigned 
char* t = utf8;

    length 
= 0;
    
while (utf8 - t < len){
        
//one byte.ASCII as a, b, c, 1, 2, 3 ect
        if ( *(unsigned char *) utf8 <= 0x7f ) {
            
//expand with 0s.
            *unicode++ = *utf8++;
        }
        
//2 byte.
        else if ( *(unsigned char *) utf8 <= 0xdf ) {
            
*unicode++ = ((*(unsigned char *) utf8 & 0x1f<< 6+ ((*(unsigned char *) (utf8 + 1)) & 0x3f);
            utf8 
+= 2;
        }
        
//3 byte.Chinese may use 3 byte.
        else {
            
*unicode++ = ((int) (*(unsigned char *) utf8 & 0x0f<< 12+
                ((
*(unsigned char *) (utf8 + 1& 0x3f<< 6+
                (
*(unsigned char *) (utf8 + 2& 0x3f);
            utf8 
+= 3;
        }
        length
++;
    }

    
*unicode = 0;
    
    
return (length);
}

lib: iconv.lib
dll: iconv.dll

last update: 2008-05-12
posted on 2008-04-14 13:23 lf426 阅读(2517) 评论(0)  编辑 收藏 引用 所属分类: mySDL_GameEngine

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