C#字节数组(byte[])和字符串相互转换方式
C#字节数组(byte[])和字符串相互转换
通过C#中的system.text.encoding获取字符串的编码可以有ASCII,DEFAULT,utf-8以及其他一些方式
对于英文而言这几种所获取的编码是没有太大区别的,而中文则大有不同,其中DEFAULT所采取的是GB2312
可以通过一下方式进行确认,程序运行后会发现bufOfGB和buf是相同的
string str = "hello,我的祖国";
byte[] bufOfGB = System.Text.Encoding.GetEncoding("gb2312").GetBytes(str);
Array.ForEach(bufOfGB,m=>Console.WriteLine(m));
Console.WriteLine(System.Text.Encoding.Default);
byte[] buf = System.Text.Encoding.Default.GetBytes(str);
Array.ForEach(buf,m=>Console.WriteLine(m));
Console.WriteLine("-------------");
byte[] bufOfASCII = System.Text.Encoding.ASCII.GetBytes(str);
Array.ForEach(bufOfASCII,m=>Console.WriteLine(m));
Console.WriteLine("-------------");
byte[] bufOfUTF = System.Text.Encoding.UTF8.GetBytes(str);
Array.ForEach(bufOfUTF,m=>Console.WriteLine(m));
Console.WriteLine("-------------");
byte[] byteArray ={43,45,67,88,23,1f}
string str = System.Text.Encoding.Default.GetString(byteArray);
C#字节数组向类型的转化
字节数组与图像
#region BytesToBmp【函数】将字节数组转化为图像
/// <summary>
/// 【函数】将字节数组转化为图像
/// </summary>
/// <param name="buffer"></param>
/// <returns></returns>
public static Image BytesToBmp(byte[] buffer)
{
MemoryStream ms = new MemoryStream(buffer);
ms.Position = 0;
Image img = Image.FromStream(ms);
ms.Close();
return img;
}
#endregion
#region BmpToBytes【函数】将图像转化为字节数组
/// <summary>
/// 【函数】将图像转化为字节数组
/// </summary>
/// <param name="Bit"></param>
/// <returns></returns>
public static byte[] BmpToBytes(Bitmap Bit)
{
byte[] back = null;
MemoryStream ms = new MemoryStream();
Bit.Save(ms, System.Drawing.Imaging.ImageFORMat.Png);
back = ms.GetBuffer();
return back;
}
#endregion
字节数组与字符串
#region StringToBytes【函数】将字符串转化为字节组
/// <summary>
/// 【函数】将字符串转化为字节组
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static byte[] StringToBytes(string str)
{
byte[] data = System.Text.Encoding.Default.GetBytes(str);
}
#endregion
#region BytesToString【函数】将字节数组转化为字符串
/// <summary>
/// 【函数】将字节数组转化为字符串
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public static string BytesToString(byte[] data)
{
string str = System.Text.Encoding.Default.GetString(data);
return str;
}
#endregion
字节数组与整数
#region BytesToInt【函数】byte数组转int数组
/// <summary>
/// 【函数】byte数组转int数组
/// </summary>
/// <param name="src">源byte数组</param>
/// <param name="offset">起始位置</param>
/// <returns></returns>
public static int[] BytesToInt(byte[] src, int offset)
{
int[] values = new int[src.Length / 4];
for (int i = 0; i < src.Length / 4; i++)
{
int value = (int)((src[offset] & 0xFF)
| ((src[offset + 1] & 0xFF) << 8)
| ((src[offset + 2] & 0xFF) << 16)
| ((src[offset + 3] & 0xFF) << 24));
values[i] = value;
offset += 4;
}
return values;
}
#endregion
#region IntToBytes【函数】int数组转byte数组
/// <summary>
/// 【函数】int数组转byte数组
/// </summary>
/// <param name="src">源int数组</param>
/// <param name="offset">起始位置,一般为0</param>
/// <returns>values</returns>
public static byte[] IntToBytes(int[] src, int offset)
{
byte[] values = new byte[src.Length * 4];
for (int i = 0; i < src.Length; i++)
{
values[offset + 3] = (byte)((src[i] >> 24) & 0xFF);
values[offset + 2] = (byte)((src[i] >> 16) & 0xFF);
values[offset + 1] = (byte)((src[i] >> 8) & 0xFF);
values[offset] = (byte)(src[i] & 0xFF);
offset += 4;
}
return values;
}
#endregion
字节数组与Object
#region ObjectToBytes【函数】将一个object对象序列化,返回一个byte[]
/// <summary>
/// 【函数】将一个object对象序列化,返回一个byte[]
/// </summary>
/// <param name="obj">能序列化的对象</param>
/// <returns></returns>
public static byte[] ObjectToBytes(object obj)
{
using (MemoryStream ms = new MemoryStream())
{
IFormatter formatter = new BinaryFormatter();
formatter.Serialize(ms, obj);
return ms.GetBuffer();
}
}
#endregion
#region BytesToObject【函数】将一个序列化后的byte[]数组还原
/// <summary>
/// 【函数】将一个序列化后的byte[]数组还原
/// </summary>
/// <param name="Bytes"></param>
/// <returns></returns>
public static object BytesToObject(byte[] Bytes)
{
using (MemoryStream ms = new MemoryStream(Bytes))
{
IFormatter formatter = new BinaryFormatter();
return formatter.Deserialize(ms);
}
}
#endregion
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持。
相关文章