asp.net string binary encoding array conversion function
- 2020-05-07 19:31:34
- OfStack
1. String to 2 array
string content=" this is a test!" ;
System.Text.UnicodeEncoding converter = new System.Text.UnicodeEncoding();
byte[] byteArr = converter.GetBytes(content);
2.2 convert the base array to a string
In programming, you will encounter the case of saving files in binary data to the database. For example, save "C:\ test.html "files to the database and read them out:
1. Save the file as a stream to the database:
2. Reading saved as a file or string from the database is the opposite of step 1
1. Convert GB2312 data to UTF-8 data as follows (other codes are equivalent) :
string content=" this is a test!" ;
System.Text.UnicodeEncoding converter = new System.Text.UnicodeEncoding();
byte[] byteArr = converter.GetBytes(content);
2.2 convert the base array to a string
System.Text.UnicodeEncoding converter = new System.Text.UnicodeEncoding();
string spcontent = converter.GetString(byteArr );
In programming, you will encounter the case of saving files in binary data to the database. For example, save "C:\ test.html "files to the database and read them out:
1. Save the file as a stream to the database:
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)
{// Here you can do something like filter the data in the file
sb.Append(sLine);
}
}
objReader.Close();
content= sb.ToString(); // If you want to display the contents of the entire file on the screen, you can use <%=content%> Put it in the right place
System.Text.UnicodeEncoding converter = new System.Text.UnicodeEncoding();
byte[] byteArr = converter.GetBytes(content);
// To insert the code into the database,
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){// Success! }
2. Reading saved as a file or string from the database is the opposite of step 1
1. Convert GB2312 data to UTF-8 data as follows (other codes are equivalent) :
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;
}