C 中的字串及其編碼轉換

2022-08-20 22:12:08 字數 1541 閱讀 8351

**自:

根據查詢的system.text.encoding類的屬性,方法寫了如下的轉換程式:

1

public

string utf8togb2312(string

str)

2...

12catch (exception ex)//

(unsupportedencodingexception ex)

13...17}

18public

string gb2312toutf8(string

str)

19...

30byte temp1 =encoding.convert(gb2312, uft8, temp);

31 messagebox.show("

uft8的編碼的位元組個數:

" +temp1.length);

32for (int i = 0; i < temp1.length; i++)

33...

36string result =uft8.getstring(temp1);

37return

result;38}

39catch (exception ex)//

(unsupportedencodingexception ex)

40...

44 }

主要使用的就是獲取編碼方式的類物件,

encoding utf8 = encoding.getencoding(65001);//使用code page

encoding gb2312 = encoding.getencoding("gb2312");//通過bodyname

獲取字元編碼位元組序列:byte temp=utf8.getbytes(str);

編碼方式轉換:byte temp1=encoding.convert(utf8, gb2312, temp);

獲取編碼的字串:string str1=gb2312.getstring(temp1);

這樣即完成了字元編碼的轉換。

encoding.default在 簡體中文os中一般是gb2312格式

網上流傳的第二種方法:

在使用mysql時會遇到中文亂碼的問題就此寫下面兩個函式  

*   

在寫入資料庫和從資料庫讀出時將編碼改變  

*   

author:alice  

*   

date       

:2006/1/25  

*/  

//寫入資料庫時進行轉換  

1

public

string gb2312_iso8859(string

write) 2

1415

//讀出時進行轉換

16public

string iso8859_gb2312(string

read)

17

C 中的字串及其編碼轉換

自 根據查詢的system.text.encoding類的屬性,方法寫了如下的轉換程式 1 public string utf8togb2312 string str 2 12 catch exception ex unsupportedencodingexception ex 13 17 18 p...

字元編碼及其轉換

1.ascii 單字儲存 ascii碼使用單字儲存,即8位儲存,所以最多只能編碼256個字元,主要為西文服務。ascii碼劃分為兩個集合 128個字元的標準ascii碼和附加的128個字元的擴充ascii碼。2.ansi 單字儲存,也可以理解為單雙字混存 ansi是ascii字符集的擴充,在儲存as...

php轉換字串的字元編碼

1 把 gbk 編碼字串轉換成 utf 8 編碼字串 view plaincopy to clipboardprint?header content type text html charset utf 8 echo mb convert encoding 你是我的好朋友 utf 8 gbk 2 把...