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就可以了