时 间 记 忆
最 新 评 论
专 题 分 类
最 新 日 志
最 新 留 言
搜 索
用 户 登 录
友 情 连 接
博 客 信 息
 
ansi到unicode的转换算法
[ 2009/7/8 19:10:00 | By: Rose2000 ]
 
普及一下:
字符必须编码后才能被计算机处理。计算机使用的缺省编码方式就是计算机的内码。早期的计算机使用7位的ASCII编码,为了处理汉字,程序员设计了用于简体中文的GB2312和用于繁体中文的big5。
Unicode也是一种字符编码方法,不过它是由国际组织设计,可以容纳全世界所有语言文字的编码方案。Unicode的学名是"Universal Multiple-Octet Coded Character Set",简称为UCS。UCS可以看作是"Unicode Character Set"的缩写。

如果要转换的话,就要涉及到具体的字符编码方式的问题了。比如GB2312和用于繁体中文的big5。转换到UNICODE方法就不一样。

同时UNICODE有两种格式:UCS-2和UCS-4,当然还有好多。就这样问怎么转好象有点......

ASCII GB2312中中文分界点0x7F




自己写的话需要建一个map,然后通过ascii来查表。这是嵌入式系统中的做法



//嵌入式系统中是这样写的


int AnsiiToUnicodeString(char *pOutBuffer, char *pInBuffer )
{

S16 count = -1;
int charLen = 0;
int arrOut[2];

while( *pInBuffer != '\0')
{

UnicodeToUCS2Encoding(int *pInBuffer,&charLen,arrOut);


//#ifdef MMI_ON_WIN32
pOutBuffer[++count] = arrOut[0];
pOutBuffer[++count] = arrOut[1];
pInBuffer++;
//#endif



}


int UnicodeToUCS2Encoding(int unicode,int *charLength,int *arrOut)
{

int status = ST_SUCCESS;
int index = 0;

//#ifdef MMI_ON_WIN32
if(arrOut != 0)
{

if( unicode < 256 )
{
arrOut[index++] = *((int*)(&unicode));
arrOut[index] = 0;

}
else
{
arrOut[index++] = *((int*)(&unicode));
arrOut[index] = *(((int*)(&unicode)) + 1);

}
*charLength = 2;
}
else
{

status = ST_FAILURE;
}
//#endif

return status;
}


pOutBuffer[++count] = '\0';
pOutBuffer[++count] = '\0';
return count + 1;
}




在实际系统中写得还有很多,那是考虑硬件的,

我做了一点删除,不过大概意思可以看得出来。



mark



S16 指的是 有符号的16位数吗?



up



在linux中的方法是调用iconv.h中定义的三个函数:iconv_open iconv iconv_close

/* Allocate descriptor for code conversion from codeset FROMCODE to
codeset TOCODE.

This function is a possible cancellation points and therefore not
marked with __THROW. */
extern iconv_t iconv_open (__const char *__tocode, __const char *__fromcode);

/* Convert at most *INBYTESLEFT bytes from *INBUF according to the
code conversion algorithm specified by CD and place up to
*OUTBYTESLEFT bytes in buffer at *OUTBUF. */
extern size_t iconv (iconv_t __cd, char **__restrict __inbuf,
size_t *__restrict __inbytesleft,
char **__restrict __outbuf,
size_t *__restrict __outbytesleft);

/* Free resources allocated for descriptor CD for code conversion.

This function is a possible cancellation points and therefore not
marked with __THROW. */
extern int iconv_close (iconv_t __cd);





查 MSDN



有现成的


_Text("")
说_T或者_TEXT的两位请看一下tchar.h这个头文件(如果你装了所谓vc)

#ifdef _UNICODE
#define __T(x) L##x
#else
#define __T(x) x
#endif

#define _T(x) __T(x)
#define _TEXT(x) __T(x)


使用 MultiByteToWideChar 这个API函数就可以的!
ANSI是和UNICODE对应的编码方式,UNICODE属于wide char,而ANSI属于multi-octet,ANSI函数代码页,不同的代码页可以编码不同的语言(这里UTF-8有点特殊),而UNICODE可以编码所有的语言。

UNICODE包括UCS2(两字节编号字符)和UCS4(四字节编号字符),目前我们用的字符用UCS2基本就可以了,就算是UCS4目前也只用了前16位的(BMP)编号。编号和编码不同,我们一般提到UNICODE其实就是指UTF-16编码,所谓UNICODE编码转换其实就是指从UTF-16到ANSI各个代码页编码(UTF-8,ASCII,GB2312/GBK,BIG5等等)的转换。

因此如果要将UNICODE和ANSI相互转换,就必须首先知道ANSI代码页(一个数字ID),否则是没有意义的。在Win32平台上系统提供了编码转换函数,MultiByteToWideChar(ANSI->UNICODE)或者WideCharToMultiByte(UNICODE->ANSI),都有一个参数用来指明当前代码页。

系统不可能去猜每一个文件的编码方式,因此系统有个默认的语言设置,其实就是设置的默认代码页。在Regional Options->Language settings for the system下的语言设置。

如果没有系统提供的这些函数,那就得自己去建立映射表。
 

发表评论:

    昵称:
    密码:
    主页:
    标题: