1.起始
在C#串口开发过程中串口读出来的数据都是byte数组类型的,byte数组不方便查看,因此经常会遇见数据转换的问题。下面就介绍一些常用的数据装换的操作。这些操作虽然很基础,但是使用是非常频繁的,如果有一个工具类将会方便很多。
2.编写工具类
首先创建一个工具类ByteArrayConvert,全部方法都用公共静态方法,方便以后使用调用。类里面的方法有
/**
*
* byte数组转字符串,字符串转byte数组
* byte数组转16进制字符串,16进制字符串转byte数组
* byte数组转10进制字符串,10进制字符串转byte数组
* byte数组转8进制字符串,8进制字符串转byte数组
* byte数组转2进制字符串,2进制字符串转byte数组
*
*/
public class ByteArrayConvert
{
/// <summary>
/// byte数组转string
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public static string byteArrayToString(byte[] data) {
return Encoding.Default.GetString(data);
}
/// <summary>
/// string转 byte数组
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public static byte[] stringToByteArray(string data)
{
return Encoding.Default.GetBytes(data);
}
/// <summary>
/// byte数组转16进制字符串
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public static string byteArrayToHexString(byte[] data)
{
StringBuilder builder = new StringBuilder();
for (int i = 0; i < data.Length; i++)
{
builder.Append(string.Format("{0:X2} ", data[i]));
}
return builder.ToString().Trim();
}
/// <summary>
/// 16进制字符串转byte数组
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public static byte[] hexStringToByteArray(string data)
{
string[] chars = data.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
byte[] returnBytes = new byte[chars.Length];
//逐个字符变为16进制字节数据
for (int i = 0; i < chars.Length; i++)
{
returnBytes[i] = Convert.ToByte(chars[i], 16);
}
return returnBytes;
}
/// <summary>
/// byte数组转10进制字符串
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public static string byteArrayToDecString(byte[] data)
{
StringBuilder builder = new StringBuilder();
for (int i = 0; i < data.Length; i++)
{
builder.Append(data[i]+" ");
}
return builder.ToString().Trim();
}
/// <summary>
/// 10进制字符串转byte数组
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public static byte[] decStringToByteArray(string data)
{
string[] chars = data.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
byte[] returnBytes = new byte[chars.Length];
//逐个字符变为10进制字节数据
for (int i = 0; i < chars.Length; i++)
{
returnBytes[i] = Convert.ToByte(chars[i], 10);
}
return returnBytes;
}
/// <summary>
/// byte数组转八进制字符串
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public static string byteArrayToOtcString(byte[] data)
{
StringBuilder builder = new StringBuilder();
for (int i = 0; i < data.Length; i++)
{
builder.Append(Convert.ToString(data[i],8)+" ");
}
return builder.ToString().Trim();
}
/// <summary>
/// 八进制字符串转byte数组
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public static byte[] otcStringToByteArray(string data)
{
string[] chars = data.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
byte[] returnBytes = new byte[chars.Length];
//逐个字符变为8进制字节数据
for (int i = 0; i < chars.Length; i++)
{
returnBytes[i] = Convert.ToByte(chars[i], 8);
}
return returnBytes;
}
/// <summary>
/// 二进制字符串转byte数组
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public static byte[] binStringToByteArray(string data)
{
string[] chars = data.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
byte[] returnBytes = new byte[chars.Length];
//逐个字符变为2进制字节数据
for (int i = 0; i < chars.Length; i++)
{
returnBytes[i] = Convert.ToByte(chars[i], 2);
}
return returnBytes;
}
/// <summary>
/// byte数组转二进制字符串
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public static string byteArrayToBinString(byte[] data)
{
StringBuilder builder = new StringBuilder();
for (int i = 0; i < data.Length; i++)
{
builder.Append(Convert.ToString(data[i], 2) + " ");
}
return builder.ToString().Trim();
}
}
3.调用工具类查看效果
工具类编辑完毕,下面用Hello World字符串来测试一下使用效果。
public void testByteArrayConvert() {
string HelloWorld = "HelloWorld";
byte[] HelloWorldByte;
Debug.WriteLine("原始=====");
Debug.WriteLine(HelloWorld);
HelloWorldByte = ByteArrayConvert.stringToByteArray(HelloWorld);
HelloWorld = ByteArrayConvert.byteArrayToString(HelloWorldByte);
Debug.WriteLine("string和byte[]=====");
Debug.WriteLine(HelloWorld);
Debug.WriteLine(HelloWorldByte);
HelloWorld = ByteArrayConvert.byteArrayToHexString(HelloWorldByte);
HelloWorldByte = ByteArrayConvert.hexStringToByteArray(HelloWorld);
Debug.WriteLine("hexstring和byte[]=====");
Debug.WriteLine(HelloWorld);
Debug.WriteLine(HelloWorldByte);
HelloWorld = ByteArrayConvert.byteArrayToDecString(HelloWorldByte);
HelloWorldByte = ByteArrayConvert.decStringToByteArray(HelloWorld);
Debug.WriteLine("decstring和byte[]=====");
Debug.WriteLine(HelloWorld);
Debug.WriteLine(HelloWorldByte);
HelloWorld = ByteArrayConvert.byteArrayToOtcString(HelloWorldByte);
HelloWorldByte = ByteArrayConvert.otcStringToByteArray(HelloWorld);
Debug.WriteLine("oecstring和byte[]=====");
Debug.WriteLine(HelloWorld);
Debug.WriteLine(HelloWorldByte);
HelloWorld = ByteArrayConvert.byteArrayToBinString(HelloWorldByte);
HelloWorldByte = ByteArrayConvert.binStringToByteArray(HelloWorld);
Debug.WriteLine("binstring和byte[]=====");
Debug.WriteLine(HelloWorld);
Debug.WriteLine(HelloWorldByte);
Debug.WriteLine("再转回helloworld=====");
HelloWorld = ByteArrayConvert.byteArrayToString(HelloWorldByte);
Debug.WriteLine(HelloWorld);
}
输出内容
原始=====
HelloWorld
string和byte[]=====
HelloWorld
System.Byte[]
hexstring和byte[]=====
48 65 6C 6C 6F 57 6F 72 6C 64
System.Byte[]
decstring和byte[]=====
72 101 108 108 111 87 111 114 108 100
System.Byte[]
oecstring和byte[]=====
110 145 154 154 157 127 157 162 154 144
System.Byte[]
binstring和byte[]=====
1001000 1100101 1101100 1101100 1101111 1010111 1101111 1110010 1101100 1100100
System.Byte[]
再转回helloworld=====
HelloWorld