c語言utf8轉gb2312
① 如何把utf-8編碼的轉換為gb2312
java中字元串轉碼,根據實際運用的環境有以下三種方式
使用Java.lang.String
這是最常用的方法,先用對應編碼獲取位元組,然後重新構造新編碼,示例代碼如下:
1
2
3
String s = "清山";
byte[] b = s.getBytes("utf-8");//編碼
String sa = new String(b, "gb2312");//解碼:用什麼字元集編碼就用什麼字元集解碼
java.io.InputStreamReader/OutputStreamWriter:橋轉換
讀寫文件的應用中,可以使用這種方式,直接在IO流構造中轉換,示例代碼如下:
1
2
3
4
InputStream is = new FileInputStream("C:/項目進度跟蹤.txt");//文件讀取
InputStreamReader isr = new InputStreamReader(is, "utf-8");//解碼
OutputStream os = new FileOutputStream("C:/項目進度跟蹤_gb2312.txt");//文件輸出
OutputStreamWriter osw = new OutputStreamWriter(os, "gb2312");//開始編碼
java.nio.Charset
使用nio中的Charset轉換字元,示例代碼如下:
1
2
3
4
Charset inSet = Charset.forName("utf-8"); // 解碼字元集
Charset outSet = Charset.forName("gb2312"); // 編碼字元集
CharsetDecoder de = inSet.newDecoder(); // 解碼器
CharsetEncoder en = outSet.newEncoder();// 編碼
② 弱弱的問一句,C語言能不能實現字元串的編碼格式轉換 GB2312toUTF-8
其實 linux 和 windows 的系統函數都是C函數,並且提供了GB2312toUTF-8的函數,所以C語言是可以實現轉碼的。以下是windows的例子:int num = ::MultiByteToWideChar(CP_ACP, 0, "你好", -1, NULL, 0);wchar_t* m_arrayShort = new wchar_t[num];::MultiByteToWideChar(CP_ACP, 0, "你好", -1, m_arrayShort, num); int len = ::WideCharToMultiByte (CP_UTF8, 0, (LPCWSTR)m_arrayShort, num, 0, 0, NULL, NULL);char *tmpPT = new char[len+1];::WideCharToMultiByte(CP_UTF8, 0, (LPCWSTR)m_arrayShort, num, tmpPT, len, NULL, NULL);tmpPT[len] = 0;
③ UTF-8怎麼轉換成GB2312
如果只有一兩個文件,用記事本打開UTF8文件,然後保存,保存時點擊「保存選項」按鈕,選擇文件格式為ANSI,在中文Windows環境下,這個新文件就是GB2312格式的了。
④ UTF-8編碼怎麼轉換成GB2312
您好,您說的是自己做的網頁嗎?如果是的話可以嘗試如下:第一步:用記事本將html網頁打開,將charset=utf-8改成charset=gb2312,然後不要保存。第二步:打開記事本「文件」--「另存為」--編碼選擇「GB2312」。第三步:將所有html的頁面,包括css樣式表和js文件都這樣該。 如果是查看網頁的話點查看——編碼——GB2312 希望對您有所幫助
⑤ 在C#中怎樣將Unicode的字元編碼轉換成gb2312字元編碼
前面做一個基於sybase的mis系統, 由於sybase的後台是cp850編碼,而.net平台不支持cp850編碼。所以在程序中所有從資料庫讀出的中文都顯示為''?''。
於是考慮在.net 平台中轉換字元編碼。於是查看了.net中字元編碼的類System.Text.Encoding
裡面支持的字元集編碼有ibm850,沒有cp850,後來查看資料才知道原來這兩個名字指的是同一種編碼規范。
於是開始進行編碼轉換,首先找到一個java的程序:
public String CP850ToGB2312(String str)
...{
try
...{
byte[] temp = str.getBytes("cp850");
String result = new String(temp, "gb2312");
return result;
}
catch (UnsupportedEncodingException ex)
...{ return null; }
}
public String GB2312ToCP850(String str)
...{
try
...{
byte[] temp = str.getBytes("gb2312");
String result = new String(temp, "cp850");
return result;
}
catch (UnsupportedEncodingException ex)
...{
return null;
}
}
然後在根據查找的System.Text.Encoding類的屬性,方法寫了如下的轉換程序:
public string UTF8ToGB2312(string str)
...{
try
...{
Encoding utf8 = Encoding.GetEncoding(65001);
Encoding gb2312 = Encoding.GetEncoding("gb2312");//Encoding.Default ,936
byte[] temp = utf8.GetBytes(str);
byte[] temp1 = Encoding.Convert(utf8, gb2312, temp);
string result = gb2312.GetString(temp1);
return result;
}
catch (Exception ex)//(UnsupportedEncodingException ex)
...{
MessageBox.Show(ex.ToString());
return null;
}
}
public string GB2312ToUTF8(string str)
...{
try
...{
Encoding uft8 = Encoding.GetEncoding(65001);
Encoding gb2312 = Encoding.GetEncoding("gb2312");
byte[] temp = gb2312.GetBytes(str);
MessageBox.Show("gb2312的編碼的位元組個數:" + temp.Length);
for (int i = 0; i < temp.Length; i++)
...{
MessageBox.Show(Convert.ToUInt16(temp[i]).ToString());
}
byte[] temp1 = Encoding.Convert(gb2312, uft8, temp);
MessageBox.Show("uft8的編碼的位元組個數:" + temp1.Length);
for (int i = 0; i < temp1.Length; i++)
...{
MessageBox.Show(Convert.ToUInt16(temp1[i]).ToString());
}
string result = uft8.GetString(temp1);
return result;
}
catch (Exception ex)//(UnsupportedEncodingException ex)
...{
MessageBox.Show(ex.ToString());
return null;
}
}
主要使用的就是獲取編碼方式的類對象,
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格式。
static void Main(string[] args)
{
FileStream fs;
string fileName = "C://test.xml";
string message = "呵呵";
string m=System.Web.HttpUtility.UrlEncode(message, System.Text.Encoding.UTF8);
fs = new FileStream(fileName, FileMode.OpenOrCreate);
StreamWriter sw = new StreamWriter(fs);
fs.Seek(0, SeekOrigin.End);
sw.WriteLine("<?xml version=/"1.0/" encoding=/"UTF-8/"?><menu>" + message + "</menu>");
sw.Close();
fs.Close();
Console.Read();
}
private static string ToGB2312(string utfInfo)
{
string gb2312Info = string.Empty;
Encoding utf8 = Encoding.UTF8;
Encoding gb2312 = Encoding.GetEncoding("gb2312");
byte[] unicodeBytes = utf8.GetBytes(utfInfo);
byte[] asciiBytes = Encoding.Convert(utf8, gb2312, unicodeBytes);
char[] asciiChars = new char[gb2312.GetCharCount(asciiBytes, 0, asciiBytes.Length)];
gb2312.GetChars(asciiBytes, 0, asciiBytes.Length, asciiChars, 0);
string gb2312info = new string(asciiChars);
return gb2312info;
}
private static string ToUTF8(string gb2312Info)
{
string utf8Info = string.Empty;
Encoding utf8 = Encoding.UTF8;
Encoding gb2312 = Encoding.GetEncoding("gb2312");
byte[] unicodeBytes = gb2312.GetBytes(gb2312Info);
byte[] asciiBytes = Encoding.Convert(gb2312, utf8, unicodeBytes);
char[] asciiChars = new char[utf8.GetCharCount(asciiBytes, 0, asciiBytes.Length)];
utf8.GetChars(asciiBytes, 0, asciiBytes.Length, asciiChars, 0);
string utf8info = new string(asciiChars);
return utf8info;
}
⑥ C# 轉換編碼 如何從utf-8轉換到gb2312
///<summary>
///GB2312轉換成UTF8
///</summary>
///<paramname="text"></param>
///<returns></returns>
publicstaticstringgb2312_utf8(stringtext)
{
//聲明字元集
System.Text.Encodingutf8,gb2312;
//gb2312
gb2312=System.Text.Encoding.GetEncoding("gb2312");
//utf8
utf8=System.Text.Encoding.GetEncoding("utf-8");
byte[]gb;
gb=gb2312.GetBytes(text);
gb=System.Text.Encoding.Convert(gb2312,utf8,gb);
//返回轉換後的字元
returnutf8.GetString(gb);
}
///<summary>
///UTF8轉換成GB2312
///</summary>
///<paramname="text"></param>
///<returns></returns>
publicstaticstringutf8_gb2312(stringtext)
{
//聲明字元集
System.Text.Encodingutf8,gb2312;
//utf8
utf8=System.Text.Encoding.GetEncoding("utf-8");
//gb2312
gb2312=System.Text.Encoding.GetEncoding("gb2312");
byte[]utf;
utf=utf8.GetBytes(text);
utf=System.Text.Encoding.Convert(utf8,gb2312,utf);
//返回轉換後的字元
returngb2312.GetString(utf);
}
以上代碼可以參考一下!
⑦ 如何將UTF8轉換為GB2312
將UTF8的文檔復制到記事本文件,保存的時候將編碼改為GB2312就可以了