編碼問題學習 2

2021-09-06 23:01:42 字數 2572 閱讀 2300

c++中string / char* ,wstring / wchar_t*

window以下

char* cname = "北京市";

// 多位元組轉化成寬字元字串!

unsigned short wsname[50] = ;

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

21271 20140 24066

linux以下

測試**例如以下:

#include #include #include #include #include #include using namespace std;

void multibyte_to_widechar_test();

void read_file(const char* fname);

void dump_uchar(unsigned char ch);

int main()

void multibyte_to_widechar_test()

else

{printf("轉化成功\n");

// wsprintf(l"%ls\n", wcstr);

printf("view1 *****\n");

for (int i=0; iread_file嘗試讀取檔案裡字串編碼內容。

chs通過vi直接生成,內容為」北京市「,,/base_profile中設定例如以下:

export lc_all="zh_cn.gbk"

所以chs檔案的編碼預設是gbk。

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建立的檔案編碼為gbk,與base_profile中設定一致。

btw linux下通過iconv將utf-8編碼檔案轉化成unicode:

iconv -f utf-8 -t gbk test.txt -o pp.txt

>>> 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以下不能輸出字串的位元組碼,

>>s

等價於,

>>print(s)

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中實驗一致,這個是肯定的。

字串編碼問題學習

1. 2. gbk2312編碼表參見:

3. unicode編碼表參見:

python編碼問題 2

先上 coding utf 8 import sys import urllib2 import re import chardet import sys print sys.getdefaultencoding html src urllib2.urlopen html read print ch...

Python2編碼問題

以下內容說的都是 python 2.x 版本 我們看到的輸入輸出都是 字元 characters 計算機 程式 並不能直接處理,需要轉化成位元組資料 bytes 因為程式只能處理 bytes 資料。例如 檔案 網路傳輸等,處理的都是 bytes 資料 二進位制數字。孤立的 byte 是毫無意義的,所...

Python2編碼問題

以下內容說的都是 python 2.x 版本 我們看到的輸入輸出都是 字元 characters 計算機 程式 並不能直接處理,需要轉化成位元組資料 bytes 因為程式只能處理 bytes 資料。例如 檔案 網路傳輸等,處理的都是 bytes 資料 二進位制數字。孤立的 byte 是毫無意義的,所...