字符编码的使用和转换(转)



字符编码的使用

字符编码主要分两种:MBCS以及 Unicode。 以 C/CC++ 为例,以 char 为单位的数组使用MBCS编码(如 ASCII,GB2312,BIG5),以wchar_t 为单位的数组使用Unicode作为编码。

比如你的程序中使用:
char szTitle[] = "窗体标题“;
此时,szTitle字符串使用的的是MBCS编码,如果用户的操作系统不是中文的Windows,你的程序将无法正常显示!

所以,要换成Unicode方式:
wchar_t szTitle[] = L"窗体标题";
此时,szTitle字符串使用的的是Unicode编码,加入你的程序以unicode方式编译,在任何语言的Windows上都能显示正常。


字符编码的转换

有时候你从外部文件读进来的字符串是MBCS编码(如GB2312),而你程序里面都是统一用Unicode处理字符串,这时候要进行字符编码转换。 Windows为我们提供了很好用的API函数 MultiByteToWideChar WideCharToMultiByte 帮我们轻松实现转换。 代码如下:

GB2312 转换成 Unicode:

CODE:
wchar_t* GB2312ToUnicode(const char* szGBString)
{
UINT nCodePage = 936; //GB2312
int nLength=MultiByteToWideChar(nCodePage,0,szGBString,-1,NULL,0);
wchar_t* pBuffer = new wchar_t[nLength+1];
MultiByteToWideChar(nCodePage,0,szGBString,-1,pBuffer,nLength);
pBuffer[nLength]=0;
return pBuffer;
}
BIG5 转换成 Unicode:

CODE:
wchar_t* BIG5ToUnicode(const char* szBIG5String)
{
UINT nCodePage = 950; //BIG5
int nLength=MultiByteToWideChar(nCodePage,0,szBIG5String,-1,NULL,0);
wchar_t* pBuffer = new wchar_t[nLength+1];
MultiByteToWideChar(nCodePage,0,szBIG5String,-1,pBuffer,nLength);
pBuffer[nLength]=0;
return pBuffer;
}
Unicode 转换成 GB2312:

CODE:
char* UnicodeToGB2312(const wchar_t* szUnicodeString)
{
UINT nCodePage = 936; //GB2312
int nLength=WideCharToMultiByte(nCodePage,0,szUnicodeString,-1,NULL,0,NULL,NULL);
char* pBuffer=new char[nLength+1];
WideCharToMultiByte(nCodePage,0,szUnicodeString,-1,pBuffer,nLength,NULL,NULL);
pBuffer[nLength]=0;
return pBuffer;
}
Unicode 转换成 BIG5:

CODE:
char* UnicodeToBIG5(const wchar_t* szUnicodeString)
{
UINT nCodePage = 950; //BIG5
int nLength=WideCharToMultiByte(nCodePage,0,szUnicodeString,-1,NULL,0,NULL,NULL);
char* pBuffer=new char[nLength+1];
WideCharToMultiByte(nCodePage,0,szUnicodeString,-1,pBuffer,nLength,NULL,NULL);
pBuffer[nLength]=0;
return pBuffer;
}
繁体和简体的相互转换

利用Unicode作为媒介,还可以做出很有意思的应用。在处理中文过程中,一个经常用到的功能就是繁体和简体的互相转换。 代码如下:

繁体中文BIG5 转换成 简体中文 GB2312

CODE:
char* BIG5ToGB2312(const char* szBIG5String)
{
LCID lcid = MAKELCID(MAKELANGID(LANG_CHINESE,SUBLANG_CHINESE_SIMPLIFIED),SORT_CHINESE_PRC);

wchar_t* szUnicodeBuff = BIG5ToUnicode(szBIG5String);
char* szGB2312Buff = UnicodeToGB2312(szUnicodeBuff);

int nLength = LCMapString(lcid,LCMAP_SIMPLIFIED_CHINESE, szGB2312Buff,-1,NULL,0);
char* pBuffer = new char[nLength + 1];
LCMapString(0x0804,LCMAP_SIMPLIFIED_CHINESE,szGB2312Buff,-1,pBuffer,nLength);
pBuffer[nLength] = 0;

delete[] szUnicodeBuff;
delete[] szGB2312Buff;
return pBuffer;
}
简体中文 GB2312 转换成 繁体中文BIG5

CODE:
char* GB2312ToBIG5(const char* szGBString)
{
LCID lcid = MAKELCID(MAKELANGID(LANG_CHINESE,SUBLANG_CHINESE_SIMPLIFIED),SORT_CHINESE_PRC);

int nLength = LCMapString(lcid,LCMAP_TRADITIONAL_CHINESE,szGBString,-1,NULL,0);
char* pBuffer=new char[nLength+1];
LCMapString(lcid,LCMAP_TRADITIONAL_CHINESE,szGBString,-1,pBuffer,nLength);
pBuffer[nLength]=0;

wchar_t* pUnicodeBuff = GB2312ToUnicode(pBuffer);
char* pBIG5Buff = UnicodeToBIG5(pUnicodeBuff);

delete[] pBuffer;
delete[] pUnicodeBuff;
return pBIG5Buff;
}

文本文件读写的编码问题

当你的程序读一个文本文件时,如何判断文件中的字符是MBCS格式还是Unicode格式? Windows定义了一个"字节顺序标记"(Byte-order Mark)的概念:当一个txt文件的前2个字节为FF FE时,这个文件里面的字符采用Unicode编码, 如果没有字节顺序标记,就是MBCS编码。更多关于字节顺序标记的说明,请看 MSDN的官方资料

晴 温度:23℃-32℃


  

前段時間給我的拼音輸入法添加了筆畫輸入、半角切換、中英文標點符號切換的功能,今天又完成了輸出繁體字的功能。通過完成輸出繁體字功能,現在對windows下漢字編碼有了一些了解。

一個完整的拼音輸入法差不多完成了。

后面還要制作幫助文件,安裝程序,還有就是想再找個好一點的詞組庫。

输入法编程-随应用程序的窗口移动

看了自由拼音输入法的源代码,发现输入法的窗口不能随着应用程序的窗口移动而移动。

经过到网上找资料,并进行调试,找到了原因。

只要在处理下面这个消息时,

case IMN_SETCOMPOSITIONWINDOW:

进行窗口移动,就可以了。

自由拼音输入法的代码如下:

case IMN_SETCOMPOSITIONWINDOW:
//设置编码窗口

if(wConversionSet & CONVERSION_SET_FOLLOW) {//光标跟随
POINT ptSrc;
SIZE szOffset;
HDC hDC;
HFONT oldFont;

ptSrc = lpIMC->cfCompForm.ptCurrentPos;
ClientToScreen(lpIMC->hWnd, &ptSrc);
hDC = GetDC(lpIMC->hWnd);
oldFont = SelectObject(hDC, hUIFont);
GetTextExtentPoint(hDC,"A",1,&szOffset);
SelectObject(hDC, oldFont);
ReleaseDC(lpIMC->hWnd,hDC);

lpUIExtra->uiComp.pt.x = ptSrc.x + szOffset.cx;
lpUIExtra->uiComp.pt.y = ptSrc.y + szOffset.cy;
MoveCompWindow(hWnd,lpUIExtra,lpIMC);//应用程序窗口移动,IME窗口跟着移动。
MoveCandWindow(hWnd,lpUIExtra,lpIMC);//这两行是后加的。

}
if (IsWindow(lpUIExtra->uiComp.hWnd))
InvalidateRect(lpUIExtra->uiComp.hWnd,NULL,FALSE);

break;

C语言字符串函数大全(转)


函数名: stpcpy
功 能: 拷贝一个字符串到另一个
用 法: char *stpcpy(char *destin, char *source);
程序例:

#include
#include

int main(void)
{
char string[10];
char *str1 = "abcdefghi";

stpcpy(string, str1);
printf("%s\n", string);
return 0;
}


函数名: strcat
功 能: 字符串拼接函数
用 法: char *strcat(char *destin, char *source);
程序例:

#include
#include

int main(void)
{
char destination[25];
char *blank = " ", *c = "C++", *Borland = "Borland";

strcpy(destination, Borland);
strcat(destination, blank);
strcat(destination, c);

printf("%s\n", destination);
return 0;
}


函数名: strchr
功 能: 在一个串中查找给定字符的第一个匹配之处\
用 法: char *strchr(char *str, char c);
程序例:

#include
#include

int main(void)
{
char string[15];
char *ptr, c = 'r';

strcpy(string, "This is a string");
ptr = strchr(string, c);
if (ptr)
printf("The character %c is at position: %d\n", c, ptr-string);
else
printf("The character was not found\n");
return 0;
}


函数名: strcmp
功 能: 串比较
用 法: int strcmp(char *str1, char *str2);
看Asic码,str1>str2,返回值 > 0;两串相等,返回0
程序例:

#include
#include

int main(void)
{
char *buf1 = "aaa", *buf2 = "bbb", *buf3 = "ccc";
int ptr;

ptr = strcmp(buf2, buf1);
if (ptr > 0)
printf("buffer 2 is greater than buffer 1\n");
else
printf("buffer 2 is less than buffer 1\n");

ptr = strcmp(buf2, buf3);
if (ptr > 0)
printf("buffer 2 is greater than buffer 3\n");
else
printf("buffer 2 is less than buffer 3\n");

return 0;
}


函数名: strncmpi
功 能: 将一个串中的一部分与另一个串比较, 不管大小写
用 法: int strncmpi(char *str1, char *str2, unsigned maxlen);
程序例:

#include
#include

int main(void)
{
char *buf1 = "BBB", *buf2 = "bbb";
int ptr;

ptr = strcmpi(buf2, buf1);

if (ptr > 0)
printf("buffer 2 is greater than buffer 1\n");

if (ptr <>

if (ptr == 0)
printf("buffer 2 equals buffer 1\n");

return 0;
}


函数名: strcpy
功 能: 串拷贝
用 法: char *strcpy(char *str1, char *str2);
程序例:

#include
#include

int main(void)
{
char string[10];
char *str1 = "abcdefghi";

strcpy(string, str1);
printf("%s\n", string);
return 0;
}


函数名: strcspn
功 能: 在串中查找第一个给定字符集内容的段
用 法: int strcspn(char *str1, char *str2);
程序例:

#include
#include
#include

int main(void)
{
char *string1 = "1234567890";
char *string2 = "747DC8";
int length;

length = strcspn(string1, string2);
printf("Character where strings intersect is at position %d\n", length);

return 0;
}


函数名: strdup
功 能: 将串拷贝到新建的位置处
用 法: char *strdup(char *str);
程序例:

#include
#include
#include

int main(void)
{
char *dup_str, *string = "abcde";

dup_str = strdup(string);
printf("%s\n", dup_str);
free(dup_str);

return 0;
}


函数名: stricmp
功 能: 以大小写不敏感方式比较两个串
用 法: int stricmp(char *str1, char *str2);
程序例:

#include
#include

int main(void)
{
char *buf1 = "BBB", *buf2 = "bbb";
int ptr;

ptr = stricmp(buf2, buf1);

if (ptr > 0)
printf("buffer 2 is greater than buffer 1\n");

if (ptr <>

if (ptr == 0)
printf("buffer 2 equals buffer 1\n");

return 0;
}

函数名: strerror
功 能: 返回指向错误信息字符串的指针
用 法: char *strerror(int errnum);
程序例:

#include
#include

int main(void)
{
char *buffer;
buffer = strerror(errno);
printf("Error: %s\n", buffer);
return 0;
}


函数名: strcmpi
功 能: 将一个串与另一个比较, 不管大小写
用 法: int strcmpi(char *str1, char *str2);
程序例:

#include
#include

int main(void)
{
char *buf1 = "BBB", *buf2 = "bbb";
int ptr;

ptr = strcmpi(buf2, buf1);

if (ptr > 0)
printf("buffer 2 is greater than buffer 1\n");

if (ptr <>

if (ptr == 0)
printf("buffer 2 equals buffer 1\n");

return 0;
}


函数名: strncmp
功 能: 串比较
用 法: int strncmp(char *str1, char *str2, int maxlen);
程序例:

#include
#include

int main(void)

{
char *buf1 = "aaabbb", *buf2 = "bbbccc", *buf3 = "ccc";
int ptr;

ptr = strncmp(buf2,buf1,3);
if (ptr > 0)
printf("buffer 2 is greater than buffer 1\n");
else
printf("buffer 2 is less than buffer 1\n");

ptr = strncmp(buf2,buf3,3);
if (ptr > 0)
printf("buffer 2 is greater than buffer 3\n");
else
printf("buffer 2 is less than buffer 3\n");

return(0);
}

函数名: strncmpi
功 能: 把串中的一部分与另一串中的一部分比较, 不管大小写
用 法: int strncmpi(char *str1, char *str2);
程序例:

#include
#include

int main(void)
{
char *buf1 = "BBBccc", *buf2 = "bbbccc";
int ptr;

ptr = strncmpi(buf2,buf1,3);

if (ptr > 0)
printf("buffer 2 is greater than buffer 1\n");

if (ptr <>

if (ptr == 0)
printf("buffer 2 equals buffer 1\n");

return 0;
}

函数名: strncpy
功 能: 串拷贝
用 法: char *strncpy(char *destin, char *source, int maxlen);
程序例:

#include
#include

int main(void)
{
char string[10];
char *str1 = "abcdefghi";

strncpy(string, str1, 3);
string[3] = '\0';
printf("%s\n", string);
return 0;
}

函数名: strnicmp
功 能: 不注重大小写地比较两个串
用 法: int strnicmp(char *str1, char *str2, unsigned maxlen);
程序例:

#include
#include

int main(void)
{
char *buf1 = "BBBccc", *buf2 = "bbbccc";
int ptr;

ptr = strnicmp(buf2, buf1, 3);

if (ptr > 0)
printf("buffer 2 is greater than buffer 1\n");

if (ptr <>

if (ptr == 0)
printf("buffer 2 equals buffer 1\n");

return 0;
}


函数名: strnset
功 能: 将一个串中的所有字符都设为指定字符
用 法: char *strnset(char *str, char ch, unsigned n);
程序例:

#include
#include

int main(void)
{
char *string = "abcdefghijklmnopqrstuvwxyz";
char letter = 'x';

printf("string before strnset: %s\n", string);
strnset(string, letter, 13);
printf("string after strnset: %s\n", string);

return 0;
}

函数名: strpbrk
功 能: 在串中查找给定字符集中的字符
用 法: char *strpbrk(char *str1, char *str2);
程序例:

#include
#include

int main(void)
{
char *string1 = "abcdefghijklmnopqrstuvwxyz";
char *string2 = "onm";
char *ptr;

ptr = strpbrk(string1, string2);

if (ptr)
printf("strpbrk found first character: %c\n", *ptr);
else
printf("strpbrk didn't find character in set\n");

return 0;
}


函数名: strrchr
功 能: 在串中查找指定字符的最后一个出现
用 法: char *strrchr(char *str, char c);
程序例:

#include
#include

int main(void)
{
char string[15];
char *ptr, c = 'r';

strcpy(string, "This is a string");
ptr = strrchr(string, c);
if (ptr)
printf("The character %c is at position: %d\n", c, ptr-string);
else
printf("The character was not found\n");
return 0;
}


函数名: strrev
功 能: 串倒转
用 法: char *strrev(char *str);
程序例:

#include
#include

int main(void)
{
char *forward = "string";

printf("Before strrev(): %s\n", forward);
strrev(forward);
printf("After strrev(): %s\n", forward);
return 0;
}

函数名: strset
功 能: 将一个串中的所有字符都设为指定字符
用 法: char *strset(char *str, char c);
程序例:

#include
#include

int main(void)
{
char string[10] = "123456789";
char symbol = 'c';

printf("Before strset(): %s\n", string);
strset(string, symbol);
printf("After strset(): %s\n", string);
return 0;
}


函数名: strspn
功 能: 在串中查找指定字符集的子集的第一次出现
用 法: int strspn(char *str1, char *str2);
程序例:

#include
#include
#include

int main(void)
{
char *string1 = "1234567890";
char *string2 = "123DC8";
int length;

length = strspn(string1, string2);
printf("Character where strings differ is at position %d\n", length);
return 0;
}

函数名: strstr
功 能: 在串中查找指定字符串的第一次出现
用 法: char *strstr(char *str1, char *str2);
程序例:

#include
#include

int main(void)
{
char *str1 = "Borland International", *str2 = "nation", *ptr;

ptr = strstr(str1, str2);
printf("The substring is: %s\n", ptr);
return 0;
}

函数名: strtod
功 能: 将字符串转换为double型值
用 法: double strtod(char *str, char **endptr);
程序例:

#include
#include

int main(void)
{
char input[80], *endptr;
double value;

printf("Enter a floating point number:");
gets(input);
value = strtod(input, &endptr);
printf("The string is %s the number is %lf\n", input, value);
return 0;
}


函数名: strtok
功 能: 查找由在第二个串中指定的分界符分隔开的单词
用 法: char *strtok(char *str1, char *str2);
程序例:

#include
#include

int main(void)
{
char input[16] = "abc,d";
char *p;

/* strtok places a NULL terminator
in front of the token, if found */
p = strtok(input, ",");
if (p) printf("%s\n", p);

/* A second call to strtok using a NULL
as the first parameter returns a pointer
to the character following the token */
p = strtok(NULL, ",");
if (p) printf("%s\n", p);
return 0;
}


函数名: strtol
功 能: 将串转换为长整数
用 法: long strtol(char *str, char **endptr, int base);
程序例:

#include
#include

int main(void)
{
char *string = "87654321", *endptr;
long lnumber;

/* strtol converts string to long integer */
lnumber = strtol(string, &endptr, 10);
printf("string = %s long = %ld\n", string, lnumber);

return 0;
}

函数名: strupr
功 能: 将串中的小写字母转换为大写字母
用 法: char *strupr(char *str);
程序例:

#include
#include

int main(void)
{
char *string = "abcdefghijklmnopqrstuvwxyz", *ptr;

/* converts string to upper case characters */
ptr = strupr(string);
printf("%s\n", ptr);
return 0;
}


函数名: swab
功 能: 交换字节
用 法: void swab (char *from, char *to, int nbytes);
程序例:

#include
#include
#include

char source[15] = "rFna koBlrna d";
char target[15];

int main(void)
{
swab(source, target, strlen(source));
printf("This is target: %s\n", target);
return 0;
}

晴 温度:22℃-30℃


昨天我的侄子开始上小学了,呵呵,跟我念的是同一所小学校,景德镇市第一小学。

时间过的真快,一恍就二十多年,没想到今天我的侄子走同样的路去上同样的小学。想起我在小学校操场上跟同学玩耍,嘻闹,升国旗,在课堂上开小猜,做小动作,跟同学在上课的时候讲话,放学的时候被老师留校,罚写课文,就象昨天的事一样。

刘显宗

温度:20℃-24℃ 中雨转小雨


今天下了雨,气温转冷了,下午工作完回家的时候,身上穿了件短袖白衬衫,路上感觉有点冷。我喜欢这样的天气,因为我这人比较贪凉,而且因为下了雨,所以空气干净,这让我觉得很舒服。