System.Text.UnicodeEncoding converter = new System.Text.UnicodeEncoding(); byte[] byteArr = converter.GetBytes(content);

2.二進制數(shù)組轉為字符串 " /> 久久夜色电影,色屁屁www国产馆在线观看,合欧美一区二区三区

色尼玛亚洲综合影院,亚洲3atv精品一区二区三区,麻豆freexxxx性91精品,欧美在线91

asp.net 字符串、二進制、編碼數(shù)組轉換函數(shù)

1.字符串轉二進制數(shù)組
string content="這是做個測試!";

System.Text.UnicodeEncoding converter = new System.Text.UnicodeEncoding();
byte[] byteArr = converter.GetBytes(content);

2.二進制數(shù)組轉為字符串
復制代碼 代碼如下:
System.Text.UnicodeEncoding converter = new System.Text.UnicodeEncoding();
string spcontent = converter.GetString(byteArr );


在編程中會遇到將文件以二進制數(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)
  {//這里可以做相應的處理,如過濾文件中的數(shù)據(jù)
   sb.Append(sLine);

   }
 }

objReader.Close();

content= sb.ToString(); //如果你要將整個文件的內容顯示在界面上,你可以用<%=content%>放到相應的地方

      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是一個相反的過程


1.將GB2312數(shù)據(jù)轉換為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;
}

AspNet技術asp.net 字符串、二進制、編碼數(shù)組轉換函數(shù),轉載需保留來源!

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 镇坪县| 瑞金市| 马尔康县| 上饶县| 乾安县| 溧水县| 全南县| 牡丹江市| 汉沽区| 二连浩特市| 通江县| 瑞丽市| 蕉岭县| 南溪县| 新野县| 娱乐| 高唐县| 定结县| 嘉荫县| 南溪县| 湘潭市| 黑水县| 大港区| 洛南县| 六枝特区| 霍城县| 兴仁县| 青川县| 抚州市| 唐海县| 宝鸡市| 孙吴县| 济源市| 秦安县| 额尔古纳市| 新巴尔虎右旗| 安徽省| 饶平县| 桐梓县| 敦煌市| 宁河县|