1、字符串转16进制字节数组
/// <summary>
/// 字符串转16进制字节数组
/// </summary>
/// <param name="hexString"></param>
/// <returns></returns>
public static byte[] strToToHexByte(string hexString)
{
hexString = hexString.Replace(" ", "");
if ((hexString.Length % 2) != 0)
hexString += " ";
byte[] returnBytes = new byte[hexString.Length / 2];
for (int i = 0; i < returnBytes.Length; i++)
returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
return returnBytes;
}
2、字节数组转16进制字符串
/// <summary>
/// 字节数组转16进制字符串
/// </summary>
/// <param name="bytes"></param>
/// <returns></returns>
public static string byteToHexStr(byte[] bytes)
{
string returnStr = "";
if (bytes != null)
{
for (int i = 0; i < bytes.Length; i++)
{
returnStr += bytes[i].ToString("X2");
}
}
return returnStr;
}
3、Stream转换为Byte数组
/// <summary>
/// 将 Stream 转成 byte[]
/// </summary>
/// <param name="stream"></param>
/// <returns></returns>
public byte[] StreamToBytes(Stream stream)
{
byte[] bytes = new byte[stream.Length];
stream.Read(bytes, 0, bytes.Length);
// 设置当前流的位置为流的开始
stream.Seek(0, SeekOrigin.Begin);
return bytes;
}
4、byte数组转换为Stream
/// <summary>
/// 将 byte[] 转成 Stream
/// </summary>
/// <param name="bytes"></param>
/// <returns></returns>
public static Stream BytesToStream(byte[] bytes)
{
Stream stream = new MemoryStream(bytes);
return stream;
}
5、 xml字体串反序列为类实体
/// <summary>
/// xml字体串反序列为类实体
/// </summary>
/// <param name="type"></param>
/// <param name="xml"></param>
/// <returns></returns>
public static object Deserialize(Type type, string xml)
{
try
{
using (StringReader sr = new StringReader(xml))
{
XmlSerializer xmldes = new XmlSerializer(type);
return xmldes.Deserialize(sr);
}
}
catch (Exception ex)
{
throw;
}
}
6、int转换为byte数组
/// <summary>
/// int转为byte数组
/// </summary>
/// <param name="n"></param>
/// <returns></returns>
public static byte[] intToBytes(int n)
{
byte[] intBuff = BitConverter.GetBytes(n);
return intBuff;
}
7、byte数组转换为int
/// <summary>
/// byte转换为int
/// </summary>
/// <param name="buffer"></param>
/// <returns></returns>
public static int ByteToInt(byte[] b)
{
//int num = BitConverter.ToInt32(buffer, 0);
//return num;
int mask = 0xff;
int temp = 0;
int n = 0;
for (int i = 0; i < b.Length; i++)
{
n <<= 8;
temp = b[i] & mask;
n |= temp;
}
return n;
}
8、long类型转成byte数组
/// <summary>
/// long类型转成byte数组
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
public static byte[] getBytes(long i)
{
byte[] buffer = BitConverter.GetBytes(i);
return buffer;
}
9、byte数组转换为long
/// <summary>
/// long类型转成byte数组
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
public static long getBytes(byte[] buffer)
{
Int64 s = BitConverter.ToInt64(buffer, 0);
return s;
}
10、合并byte数组
/// <summary>
/// 合并数组byte[]
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
/// <returns></returns>
public static byte[] copybyte(byte[] a, byte[] b)
{
byte[] c = new byte[a.Length + b.Length];
a.CopyTo(c, 0);
b.CopyTo(c, a.Length);
return c;
}
11、比较2个byte数组是否相等
/// <summary>
/// 数组比较是否相等
/// </summary>
/// <param name="bt1">数组1</param>
/// <param name="bt2">数组2</param>
/// <returns>true:相等,false:不相等</returns>
public static bool CompareArray(byte[] bt1, byte[] bt2)
{
var len1 = bt1.Length;
var len2 = bt2.Length;
if (len1 != len2)
{
return false;
}
for (var i = 0; i < len1; i++)
{
if (bt1[i] != bt2[i])
return false;
}
return true;
}
12、时间格式转换为byte数组
/// <summary>
/// 把时间转换为字节数组
/// </summary>
/// <param name="time"></param>
/// <returns></returns>
public static byte[] DateBytes(DateTime time)
{
byte[] bytes= BitConverter.GetBytes(time.ToBinary());
return bytes;
}
13、string转换为byte数组
byte[] byteArray = System.Text.Encoding.Default.GetBytes(str);
14、byte数组转换为string
string str = System.Text.Encoding.Default.GetString (buffer);