public class Util
{
/// <summary>
/// byte转换成string
/// </summary>
/// <param name="bts"></param>
/// <returns></returns>
public static string ByteToString(byte[] bts)
{
if(bts==null||bts.Length<=0)
{
return null;
}
Encoding cod=Encoding.UTF8;
return cod.GetString(bts);
}
/// <summary>
/// string转换成byte
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static byte[] StringToByte(string str)
{
if(string.IsNullOrEmpty(str))
{
return null;
}
Encoding cod = Encoding.UTF8;
return cod.GetBytes(str);
}
/// <summary>
/// byte转换成stream
/// </summary>
/// <param name="bts"></param>
/// <returns></returns>
public static Stream ByteToStream(byte[] bts)
{
return new MemoryStream(bts);
}
/// <summary>
/// stream转换成byte
/// </summary>
/// <param name="stream"></param>
/// <returns></returns>
public static byte[] StreamToByte(Stream stream)
{
MemoryStream ms = new MemoryStream();
var buff = new byte[0x100];
for(var i= stream.Read(buff,0,buff.Length);i>0;i= stream.Read(buff,0,buff.Length))
{
ms.Write(buff, 0, i);
}
stream.Close();
return ms.ToArray();
}
/// <summary>
/// stream转换成byte
/// </summary>
/// <param name="stream"></param>
/// <returns></returns>
public static byte[] StreamToByte1(Stream stream)
{
byte[] bt = new byte[stream.Length];
stream.Read(bt, 0, bt.Length);
stream.Seek(0, SeekOrigin.Begin);
return bt;
}
/// <summary>
/// stream转换成文件
/// </summary>
/// <param name="stream"></param>
/// <param name="fileName"></param>
public static void StreamToFile(Stream stream,string fileName)
{
FileStream file = new FileStream(fileName, FileMode.Create);
byte[] bt = new byte[stream.Length];
stream.Read(bt, 0, bt.Length);
stream.Seek(0, SeekOrigin.Begin);
BinaryWriter bw = new BinaryWriter(file);
bw.Write(bt);
bw.Close();
file.Close();
}
/// <summary>
/// file转换成stream
/// </summary>
/// <param name="fileName"></param>
/// <returns></returns>
public static Stream FileToStream(string fileName)
{
FileStream file = new FileStream(fileName, FileMode.Open);
byte[] bt = new byte[file.Length];
file.Read(bt, 0, bt.Length);
file.Close();
Stream stream = new MemoryStream(bt);
return stream;
}
/// <summary>
/// 获取文件的哈希散列值
/// </summary>
/// <param name="bts"></param>
/// <returns></returns>
public static string MD5Hash(byte[] bts)
{
var md5Csp = new System.Security.Cryptography.MD5CryptoServiceProvider();
byte[] md5Out= md5Csp.ComputeHash(bts);
return BitConverter.ToString(md5Out).Replace("-", "").ToLower();
}
/// <summary>
/// 获取字符串的哈希散列值
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static string StringToMDHash(string str)
{
if(string.IsNullOrEmpty(str))
{
return null;
}
var md5Csp = new System.Security.Cryptography.MD5CryptoServiceProvider();
byte[] md5Out = md5Csp.ComputeHash(Encoding.UTF8.GetBytes(str));
return BitConverter.ToString(md5Out).Replace("-", "").ToLower();
}
/// <summary>
/// byte压缩
/// </summary>
/// <param name="compress"></param>
/// <returns></returns>
public static byte[] Compress(byte[] uncompress)
{
MemoryStream ms = new MemoryStream();
using (GZipStream gzs = new GZipStream(ms, CompressionMode.Compress))
{
gzs.Write(uncompress, 0, uncompress.Length);
gzs.Close();
return ms.ToArray();
}
}
/// <summary>
/// byte解压
/// </summary>
/// <param name="compress"></param>
/// <returns></returns>
public static byte[] Decompress(Stream stream)
{
var buff = new byte[4096];
GZipStream gzs = new GZipStream(stream, CompressionMode.Decompress);
using (MemoryStream ms = new MemoryStream())
{
for(var r=-1;r!=0;r=gzs.Read(buff,0,buff.Length))
{
if(r>0)
{
ms.Write(buff, 0, r);
}
}
return ms.ToArray();
}
}
}
这篇文章主要是string和byte数组之间的转换,stream和byte之间的转换,byte,stream的解压与压缩,还有文件和stream之间的转变