编码问题学习【2】
多字節(jié)和寬字符
C++中string / char* ,wstring / wchar_t*
C++測試
window以下
char* cName = "北京市"; // 多字節(jié)轉化成寬字符字符串! unsigned short wsName[50] = {0}; int wideCharCount = MultiByteToWideChar(CP_ACP, 0, (LPSTR)cName, -1, NULL, 0) - 1; MultiByteToWideChar(CP_ACP, 0, (LPSTR)cName, -1, (LPWSTR)wsName, wideCharCount + 1);for (int i=0; i<wideCharCount; i++) {printf("%d ", wsName[i]); }printf("\n");輸出 21271 20140 24066Linux以下
測試代碼例如以下: #include <stdlib.h> #include <stdio.h> #include <string.h>#include <locale.h> #include <iostream> #include <string> using namespace std;void multibyte_to_widechar_test(); void read_file(const char* fname); void dump_uchar(unsigned char ch);int main() {multibyte_to_widechar_test();read_file("chs");printf("any key pressed to exit...\n");getchar();return 0; }void multibyte_to_widechar_test() {typedef string str_t;str_t cur_loc = setlocale(LC_ALL, NULL);printf("cur_locale = %s\n", cur_loc.c_str()); setlocale(LC_ALL, "zh_CN.GBK"); char mb_buf[100];strcpy(mb_buf, "北京市");int mbstr_len = strlen(mb_buf);wchar_t* wcstr = NULL; int wcstr_len = mbstowcs(wcstr, mb_buf, 0) + 1; printf("mb_len = %d, wc_len = %d\n", mbstr_len, wcstr_len);wcstr = new wchar_t[wcstr_len];int ret = mbstowcs(wcstr, mb_buf, mbstr_len);if (ret <= 0){printf("轉化失敗\n");}else {printf("轉化成功\n");// wsprintf(L"%ls\n", wcstr);printf("view1 =====\n");for (int i=0; i<wcstr_len - 1; i++) { int code = (int)wcstr[i];printf("%d\t", code);} printf("\n");printf("view2 =====\n");for (int i=0; i<wcstr_len - 1; i++) { int code = (int)wcstr[i];dump_uchar( (unsigned char)(code/256) );dump_uchar( (unsigned char)(code%256) );} printf("\n");}setlocale(LC_ALL, cur_loc.c_str());}void dump_uchar(unsigned char ch) {const char* str = "0123456789abcdef";printf("0x%c%c\t", str[ch/16], str[ch%16]); }void read_file(const char* fname) {FILE* fp = fopen(fname, "r");if (!fp){return;}printf("===============\n");char buffer[100] = {0};fgets(buffer, 100, fp);printf("%s", buffer);printf("view1 =========== \n");int len = strlen(buffer) - 1;for (int i=0; i<len; i++){dump_uchar((unsigned char)buffer[i]);}printf("\n");printf("view2 =========== \n");for (int i=0; i<len; i+=2){unsigned char down = (unsigned char)buffer[i];unsigned char high = (unsigned char)buffer[i+1];printf("%d ", (high<<8)|down);} printf("\n");fclose(fp); }multibyte_to_widechar_test函數(shù)將多字節(jié)編碼轉化成unicode編碼。然后輸出unicode串內容。 read_file嘗試讀取文件里字符串編碼內容。chs通過vi直接生成,內容為”北京市“,,/base_profile中設置例如以下: export LC_ALL="zh_CN.GBK"所以chs文件的編碼默認是gbk。
g++ test.cpp -o app_test,然后執(zhí)行輸出: root@h10-xx-xx-xx:~/peteryfren/cpp/encode_app> ./app_test cur_locale = C mb_len = 6, wc_len = 4 轉化成功 view1 ===== 21271 20140 24066 view2 ===== 0x53 0x17 0x4e 0xac 0x5e 0x02 =============== 北京市 view1 =========== 0xb1 0xb1 0xbe 0xa9 0xca 0xd0 view2 =========== 45489 43454 53450 any key pressed to exit...“北京市”的unicode編碼值與window上輸出一致。“北京市”的gbk2312編碼為45489,43454,53450。 同一時候linux vi創(chuàng)建的文件編碼為gbk,與base_profile中設置一致。
BTW linux下通過iconv將utf-8編碼文件轉化成unicode: iconv -f UTF-8 -t GBK test.txt -o pp.txt
python2.7測試
>>> s = u'北京市' >>> s u'\u5317\u4eac\u5e02' >>> gbks = '北京市' >>> gbks '\xb1\xb1\xbe\xa9\xca\xd0' >>> s.encode('utf-8') '\xe5\x8c\x97\xe4\xba\xac\xe5\xb8\x82' 2.7以下加u表示unicode編碼,不加u使用了gbk編碼。 python3.3以下不能輸出字符串的字節(jié)碼, >>s 等價于, >>print(s)windows文本編碼驗證
1. ANSI 使用windows自帶的記事本創(chuàng)建一個默認的txt,叫npd.txt 用UE打開,16進制視圖下:此時文件里中文編碼為gbk2312編碼。與Linux上文件編碼輸出一致。
2. unicode 記事本打開npd.txt,然后另存為,此時能夠看到編碼是ANSI,選擇unicode,另存為npd_u.txt
unicode編碼,內容與上文中windows、linux上輸出一致。
3,utf-8 相同打開npd.txt,另存為,編碼選擇utf-8,另存為npd_utf8.txt
utf-8輸出與python中實驗一致,這個是肯定的。
字符串編碼問題學習?http://blog.csdn.net/ryfdizuo/article/details/17324051
gb18030和通常的gdk都是對gb2312的擴展,全部已經(jīng)包括在gb2312中的漢字編碼保持不變。
參考
1.?http://blog.csdn.net/xiaobai1593/article/details/7063535
2. GBK2312編碼表參見:http://ff.163.com/newflyff/gbk-list/
3. unicode編碼表參見:http://jlqzs.blog.163.com/blog/static/2125298320070101826277/
轉載于:https://www.cnblogs.com/mengfanrong/p/4489698.html
總結
- 上一篇: [软件项目管理]从业余人士往专家进军的头
- 下一篇: 大雁归来好句赏析70句