本篇文章给大家谈谈gb2312转换utf8c语言,以及gb2312转utf8 c语言对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
1、谁有GB2312转换成UTF-8的c代码?2、C#将字符转换成utf8编码 GB321编码转换3、C# GB2312转UTF84、怎样将gb2312编码的字符串转换为utf-8编码的字符串5、UTF-8和GB2312互转的最简单快捷的方法6、弱弱的问一句,C语言能不能实现字符串的编码格式转换 GB2312toUTF-8?
谁有GB2312转换成UTF-8的c代码?
void GB2312ToUTF_8(string pOut,char *pText, int pLen)
{
char buf[4];
char* rst = new char[pLen + (pLen 2) + 2];
memset(buf,0,4);
memset(rst,0,pLen + (pLen 2) + 2);
int i = 0;
int j = 0;
while(i pLen)
{
//如果是英文直接复制就可以
if( *(pText + i) = 0)
{
rst[j++] = pText[i++];
}
else
{
WCHAR pbuffer;
Gb2312ToUnicode(pbuffer,pText+i);
UnicodeToUTF_8(buf,pbuffer);
unsigned short int tmp = 0;
tmp = rst[j] = buf[0];
tmp = rst[j+1] = buf[1];
tmp = rst[j+2] = buf[2];
j += 3;
i += 2;
}
}
rst[j] = ”\0”;
//返回结果
pOut = rst;
delete []rst;
return;
}
C#将字符转换成utf8编码 GB321编码转换
public static string get_uft8(string unicodeString)
{
UTF8Encoding utf8 = new UTF8Encoding();
Byte[] encodedBytes = utf8.GetBytes(unicodeString);
String decodedString = utf8.GetString(encodedBytes);
return decodedString;
}
这边我以big5转换gb2312为例
Encoding big5 =Encoding.GetEncoding(“big5”);
Encoding gb2312 = Encoding.GetEncoding(“gb2312”);
byte[] big5b= big5.GetBytes(“编程无悔!”);
//关键也就是这句了
byte[] gb2312b= Encoding.Convert(big5,gb2312,big5b);
string strGb2312 = gb2312.GetString(gb2312b)
//写文件使用默认的即可,这样就能适应所有计算机了。
filePath = Path.GetDirectoryName(Application.ExecutablePath) + “\CSVFiles\” + DateTime.Now.ToString(“yyyy-MM-dd_HHmmss”) + “.csv”;
fs = new FileStream(filePath, FileMode.Create);
sw = new StreamWriter(fs, Encoding.Default);
C# GB2312转UTF8
比如实现Utf-8和GB2312的转换:
string gb2312info = “你好!”;
string utfinfo = string.Empty;
Encoding utf8 = Encoding.UTF8;
Encoding gb2312 = Encoding.GetEncoding(“gb2312”);
// Convert the string into a byte[].
byte[] unicodeBytes = gb2312 .GetBytes(gb2312info );
// Perform the conversion from one encoding to the other.
byte[] asciiBytes = Encoding.Convert(gb2312,utf8, unicodeBytes);
// Convert the new byte[] into a char[] and then into a string.
// This is a slightly different approach to converting to illustrate
// the use of GetCharCount/GetChars.
char[] asciiChars = new char[utf8 .GetCharCount(asciiBytes, 0, asciiBytes.Length)];
utf8 .GetChars(asciiBytes, 0, asciiBytes.Length, asciiChars, 0);
utfinfo = new string(asciiChars);
怎样将gb2312编码的字符串转换为utf-8编码的字符串
Java中字符串转码,根据实际运用的环境有以下三种方式
使用Java.lang.String这是最常用的方法,先用对应编码获取字节,然后重新构造新编码,示例代码如下:String s = “清山”;
byte[] b = s.getBytes(“utf-8”);//编码
String sa = new String(b, “gb2312”);//解码:用什么字符集编码就用什么字符集解码
java.io.InputStreamReader/OutputStreamWriter:桥转换读写文件的应用中,可以使用这种方式,直接在IO流构造中转换,示例代码如下:
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转换字符,示例代码如下:Charset inSet = Charset.forName(“utf-8”); // 解码字符集
Charset outSet = Charset.forName(“gb2312”); // 编码字符集
CharsetDecoder de = inSet.newDecoder(); // 解码器
CharsetEncoder en = outSet.newEncoder();// 编码
UTF-8和GB2312互转的最简单快捷的方法
下面介绍一种UTF-8和GB2312互转的最简单快捷的方法:
一、如果你想把utf-8转为GB2312
1、用记事本打开源码,把meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ /换成meta http-equiv=Content-Type content=”text/html;charset=gb2312″;如果是JS不需要加这句,如果是网页最好加上这句和你页面对应的编码。
2、用记事本打开源码,另存为,编码 哪里选择 ANSI 即可。
二、如果你想把GB2312转为utf-8
1、用记事本打开源码,把meta http-equiv=Content-Type content=”text/html;charset=gb2312″换成meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ /;如果是JS不需要加这句,如果是网页最好加上这句和你页面对应的编码。
2、用记事本打开源码,另存为,编码 哪里选择 UTF-8 即可。
特别提醒:CSS的编码最好和网页的编码一致,因为有一个古老的浏览器叫IE6,他的市场占用率依然非常高,编码不一致的CSS在IE6下不起作用。
弱弱的问一句,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;
关于gb2312转换utf8c语言和gb2312转utf8 c语言的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。