C#二进制数据转换为字符串
本文关键字:字符串 转换 数据 二进制 | 更新日期: 2023-09-27 17:58:19
这是交易。我找到了一个源代码,并对其进行了一点更改,这样我就可以从com6上的接收器中检索数据。我收到的数据是二进制的。我想要的是把它转换成一个字符串,这样我就可以剪切字符串的一部分并分别解码。我怎么能点他的?源代码在下面。
using System;
using System.IO.Ports;
using System.Threading;
public class PortChat
{
static bool _continue;
static SerialPort _serialPort;
public static void Main()
{
string name;
string message;
StringComparer stringComparer = StringComparer.OrdinalIgnoreCase;
Thread readThread = new Thread(Read);
// Create a new SerialPort object with default settings.
_serialPort = new SerialPort();
// Allow the user to set the appropriate properties.
_serialPort.PortName = SetPortName(_serialPort.PortName);
_serialPort.BaudRate = SetPortBaudRate(_serialPort.BaudRate);
_serialPort.Parity = SetPortParity(_serialPort.Parity);
_serialPort.DataBits = SetPortDataBits(_serialPort.DataBits);
_serialPort.StopBits = SetPortStopBits(_serialPort.StopBits);
_serialPort.Handshake = SetPortHandshake(_serialPort.Handshake);
// Set the read/write timeouts
_serialPort.ReadTimeout = 1000;
_serialPort.WriteTimeout = 1000;
_serialPort.Open();
_continue = true;
readThread.Start();
Console.Write("Name: ");
name = Console.ReadLine();
Console.WriteLine("Type QUIT to exit");
while (_continue)
{
message = Console.ReadLine();
if (stringComparer.Equals("quit", message))
{
_continue = false;
}
else
{
_serialPort.WriteLine(
String.Format("<{0}>: {1}", name, message));
}
}
readThread.Join();
_serialPort.Close();
}
public static void Read()
{
while (_continue)
{
try
{
string message = _serialPort.ReadLine();
Console.WriteLine(message);
}
catch (TimeoutException) { }
}
}
public static string SetPortName(string defaultPortName)
{
string portName;
portName = "COM6";
return portName;
}
public static int SetPortBaudRate(int defaultPortBaudRate)
{
string baudRate;
baudRate = "9600";
return int.Parse(baudRate);
}
public static Parity SetPortParity(Parity defaultPortParity)
{
string parity;
parity = "None";
return (Parity)Enum.Parse(typeof(Parity), parity);
}
public static int SetPortDataBits(int defaultPortDataBits)
{
string dataBits;
dataBits = "8";
return int.Parse(dataBits);
}
public static StopBits SetPortStopBits(StopBits defaultPortStopBits)
{
string stopBits;
stopBits = "One";
return (StopBits)Enum.Parse(typeof(StopBits), stopBits);
}
public static Handshake SetPortHandshake(Handshake defaultPortHandshake)
{
string handshake;
handshake = "None";
return (Handshake)Enum.Parse(typeof(Handshake), handshake);
}
}
来自端口的数据总是以二进制(字节)形式出现,因此这取决于如何解释数据。假设字节是ASCII,您可以将其编码为如下字符串:
byte[] binaryData ; // assuming binaryData contains the bytes from the port.
string ascii = Encoding.ASCII.GetString(binaryData);
如果使用新的BinaryData类,则只需要调用其ToString()方法,该方法在内部使用UTF8将字节转换为字符串。查看MSDN文档
byte[] bytes; // assuming you have got byte data
BinaryData binaryData = new BinaryData(bytes);
string message = binaryData.ToString();
如果您在字节数组中只有字节,那么请注意使用ASCII,它会限制字符串不包含特殊字符。您应该始终使用UTF8执行此操作。
byte[] bytes; // assuming you have got byte data
string message = Encoding.UTF8.GetString(bytes)