標籤:
多位元組和寬字元
C++中string / char* ,wstring / wchar_t*
C++測試
window以下
char* cName = "北京市";// 多位元組轉化成寬字元字串!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 24066
Linux以下
測試代碼例如以下:
#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函數將多位元組編碼轉化成unicode編碼。然後輸出unicode串內容。read_file嘗試讀取檔案裡字串編碼內容。
chs通過vi直接產生,內容為”北京市“,,/base_profile中設定例如以下:
export LC_ALL="zh_CN.GBK"
所以chs檔案的編碼預設是gbk。
g++ test.cpp -o app_test,然後執行輸出:
[email protected]:~/peteryfren/cpp/encode_app> ./app_test cur_locale = Cmb_len = 6, wc_len = 4轉化成功view1 =====21271 20140 24066view2 =====0x53 0x17 0x4e 0xac 0x5e 0x02===============北京市view1 =========== 0xb1 0xb1 0xbe 0xa9 0xca 0xd0view2 =========== 45489 43454 53450 any key pressed to exit...
“北京市”的unicode編碼值與window上輸出一致。“北京市”的gbk2312編碼為45489,43454,53450。同一時候linux vi建立的檔案編碼為gbk,與base_profile中設定一致。
BTW linux下通過iconv將utf-8編碼檔案轉化成unicode:
iconv -f UTF-8 -t GBK test.txt -o pp.txt
python2.7測試
>>> s = u'北京市'>>> su'\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以下不能輸出字串的位元組碼,>>s等價於,>>print(s)
windows文本編碼驗證1. ANSI使用windows內建的記事本建立一個預設的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的擴充,全部已經包括在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/
編碼問題學習【2】