asp.net 字符串、二進(jìn)制、編碼數(shù)組轉(zhuǎn)換函數(shù)
更新時(shí)間:2010年01月14日 00:25:53 作者:
字符串和二進(jìn)制數(shù)組轉(zhuǎn)換、將HTML文件顯示為頁面的一部分、UTF8和GB2312之間的轉(zhuǎn)換
1.字符串轉(zhuǎn)二進(jìn)制數(shù)組
string content="這是做個(gè)測試!";
System.Text.UnicodeEncoding converter = new System.Text.UnicodeEncoding();
byte[] byteArr = converter.GetBytes(content);
2.二進(jìn)制數(shù)組轉(zhuǎn)為字符串
System.Text.UnicodeEncoding converter = new System.Text.UnicodeEncoding();
string spcontent = converter.GetString(byteArr );
在編程中會遇到將文件以二進(jìn)制數(shù)據(jù)保存到數(shù)據(jù)庫的情況,以將"C:\test.html"文件保存到數(shù)據(jù)庫和讀取出來為例:
1.將文件以流的形式保存到數(shù)據(jù)庫中:
int itag=0;
string content = "";
StringBuilder sb = new StringBuilder();
string fileName = @"c:\test.html";
StreamReader objReader = new StreamReader(fileName, System.Text.Encoding.GetEncoding("gb2312"));
string sLine = "";
while (sLine != null)
{
sLine = objReader.ReadLine();
if (sLine != null)
{//這里可以做相應(yīng)的處理,如過濾文件中的數(shù)據(jù)
sb.Append(sLine);
}
}
objReader.Close();
content= sb.ToString(); //如果你要將整個(gè)文件的內(nèi)容顯示在界面上,你可以用<%=content%>放到相應(yīng)的地方
System.Text.UnicodeEncoding converter = new System.Text.UnicodeEncoding();
byte[] byteArr = converter.GetBytes(content);
//下面為插入到數(shù)據(jù)庫代碼,
strInsertCmd = "insert into Document (DocumentID,DocumentContent,addtime,MODITIME,status) values ('" + DocumentID + "',?,'" + NOWTIME + "','" + NOWTIME + "',' 00 ')";
cmd=new OleDbCommand(strInsertCm,ConnectionOBJ);
param = new OleDbParameter("DocumentContent", OleDbType.VarBinary, byteArr.Length, ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Current, byteArr);
cmd.Parameters.Add(param);
itag=cmd.ExecuteNonQuery();
if(itag>0){//成功!}
2.從數(shù)據(jù)庫中讀取保存為文件或者字符串和步驟1是一個(gè)相反的過程
1.將GB2312數(shù)據(jù)轉(zhuǎn)換為UTF-8數(shù)據(jù)如下(其他的編碼類推):
public string GB2312ToUTF8(string sSourse) {
string Utf8_info = string.Empty;
Encoding utf8 = Encoding.UTF8;
Encoding gb2312 = Encoding.GetEncoding("gb2312");
byte[] unicodeBytes = gb2312.GetBytes(sSourse);
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);
Utf8_info = new string(asciiChars);
return Utf8_info;
}
string content="這是做個(gè)測試!";
System.Text.UnicodeEncoding converter = new System.Text.UnicodeEncoding();
byte[] byteArr = converter.GetBytes(content);
2.二進(jìn)制數(shù)組轉(zhuǎn)為字符串
復(fù)制代碼 代碼如下:
System.Text.UnicodeEncoding converter = new System.Text.UnicodeEncoding();
string spcontent = converter.GetString(byteArr );
在編程中會遇到將文件以二進(jìn)制數(shù)據(jù)保存到數(shù)據(jù)庫的情況,以將"C:\test.html"文件保存到數(shù)據(jù)庫和讀取出來為例:
1.將文件以流的形式保存到數(shù)據(jù)庫中:
復(fù)制代碼 代碼如下:
int itag=0;
string content = "";
StringBuilder sb = new StringBuilder();
string fileName = @"c:\test.html";
StreamReader objReader = new StreamReader(fileName, System.Text.Encoding.GetEncoding("gb2312"));
string sLine = "";
while (sLine != null)
{
sLine = objReader.ReadLine();
if (sLine != null)
{//這里可以做相應(yīng)的處理,如過濾文件中的數(shù)據(jù)
sb.Append(sLine);
}
}
objReader.Close();
content= sb.ToString(); //如果你要將整個(gè)文件的內(nèi)容顯示在界面上,你可以用<%=content%>放到相應(yīng)的地方
System.Text.UnicodeEncoding converter = new System.Text.UnicodeEncoding();
byte[] byteArr = converter.GetBytes(content);
//下面為插入到數(shù)據(jù)庫代碼,
strInsertCmd = "insert into Document (DocumentID,DocumentContent,addtime,MODITIME,status) values ('" + DocumentID + "',?,'" + NOWTIME + "','" + NOWTIME + "',' 00 ')";
cmd=new OleDbCommand(strInsertCm,ConnectionOBJ);
param = new OleDbParameter("DocumentContent", OleDbType.VarBinary, byteArr.Length, ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Current, byteArr);
cmd.Parameters.Add(param);
itag=cmd.ExecuteNonQuery();
if(itag>0){//成功!}
2.從數(shù)據(jù)庫中讀取保存為文件或者字符串和步驟1是一個(gè)相反的過程
1.將GB2312數(shù)據(jù)轉(zhuǎn)換為UTF-8數(shù)據(jù)如下(其他的編碼類推):
復(fù)制代碼 代碼如下:
public string GB2312ToUTF8(string sSourse) {
string Utf8_info = string.Empty;
Encoding utf8 = Encoding.UTF8;
Encoding gb2312 = Encoding.GetEncoding("gb2312");
byte[] unicodeBytes = gb2312.GetBytes(sSourse);
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);
Utf8_info = new string(asciiChars);
return Utf8_info;
}
您可能感興趣的文章:
相關(guān)文章
asp.net音頻轉(zhuǎn)換之.amr轉(zhuǎn).mp3(利用七牛轉(zhuǎn)換法)
相信很多人都遇到amr格式的音頻文件不能直接在網(wǎng)頁播放的問題,有人使用QuickTime插件的輔助,下面這篇文章主要給大家介紹了asp.net音頻轉(zhuǎn)換之利用七牛轉(zhuǎn)換法將.amr格式轉(zhuǎn).mp3格式,需要的朋友可以參考借鑒,下面來一起看看吧。2016-12-12asp.net 擴(kuò)展GridView 增加單選按鈕列的代碼
asp.net 擴(kuò)展GridView 增加單選按鈕列的代碼2010-02-02asp.net2.0如何加密數(shù)據(jù)庫聯(lián)接字符串
asp.net2.0如何加密數(shù)據(jù)庫聯(lián)接字符串...2006-09-09使用微信PC端的截圖dll庫實(shí)現(xiàn)微信截圖功能
這篇文章主要為大家詳細(xì)介紹了使用微信PC端的截圖dll庫實(shí)現(xiàn)微信截圖功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06Asp.net的服務(wù)器推技術(shù) (Server Push)
在以往的和服務(wù)器端通信技術(shù)中,我們多數(shù)使用的是AJAX輪詢式訪問,也就是在Javascript中控制時(shí)間間隔,然后每隔一段時(shí)間就訪問一次服務(wù)器,然后獲得數(shù)據(jù)或通知。但是這種輪詢方式的訪問有90%是在做無用功。2010-01-01