怎么样将文本字符串与二进制互转?
/// <summary>
/// 将文本字符串转换成带,号分离的二进制字符串
/// </summary>
/// <param name="strContent">文本字符串</param>
/// <returns>带,号分离的二进制字符串</returns>
private string strTextTostrBin(string strText)
{
byte[] bytearr=null;
string stringtobin="";
System.Text.Encoding encoding = System.Text.Encoding.UTF8;
bytearr=encoding.GetBytes(strText);
for(int i=0;i<bytearr.Length;i++)
{
stringtobin+=","+bytearr[i].ToString();
}
return stringtobin.Substring(1);
}
/// <summary>
/// 将带,号分离的二进制字符串转换成文本字符串
/// </summary>
/// <param name="strBin">带,号分离的二进制字符串</param>
/// <returns>文本字符串</returns>
private string strBinTostrText(string strBin)
{
string [] bintostr=strBin.Split(',');
Array binArray=Array.CreateInstance(Type.GetType("System.Byte"),bintostr.Length);
for(int i=binArray.GetLowerBound(0);i<=binArray.GetUpperBound(0);i++)
{
binArray.SetValue(byte.Parse(bintostr[i]+""),i);
}
byte[] strtobin=new byte[bintostr.Length];
for(int i=binArray.GetLowerBound(0);i<=binArray.GetUpperBound(0);i++)
{
strtobin[i]=(byte)binArray.GetValue(i);
}
System.Text.Encoding encoding = System.Text.Encoding.UTF8;
return encoding.GetString(strtobin);
}
本文地址:http://www.45fan.com/a/question/68568.html