C#二进制数据转换成字符串 [英] C# binary data conversion to string
本文介绍了C#二进制数据转换成字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
下面是这笔交易。我发现了一个源代码,并改变它一点点,所以我可以从一个接收器,是COM6检索数据。这些数据我收到的二进制文件。我想要的是将其转换为字符串,这样我就可以削减部分字符串,并将其单独解码。我怎么能点自己?
的源代码是下面。
使用系统;使用System.IO.Ports
;
使用的System.Threading;
公共类PortChat
{
静态布尔_continue;
静态的SerialPort _serialPort;
公共静态无效的主要()
{
字符串名称;
字符串消息;
StringComparer stringComparer = StringComparer.OrdinalIgnoreCase;
螺纹readThread =新主题(读取);
//创建使用默认设置一个新的SerialPort对象。
_serialPort =新的SerialPort();
//允许用户设置相应的属性。
_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);
//设置读/写超时
_serialPort.ReadTimeout = 1000;
_serialPort.WriteTimeout = 1000;
_serialPort.Open();
_continue = TRUE;
readThread.Start();
Console.Write(姓名:);
NAME =到Console.ReadLine();
Console.WriteLine(请键入quit退出);
,而(_continue)
{
=消息到Console.ReadLine();
如果(stringComparer.Equals(跳槽,消息))
{
_continue = FALSE;
}
,否则
{
_serialPort.WriteLine(
的String.Format(< {0}计算值:{1},名称,消息)) ;
}
}
readThread.Join();
_serialPort.Close();
}
公共静态无效阅读()
{
,而(_continue)
{
试
{
字符串消息= _serialPort.ReadLine();
Console.WriteLine(消息);
}
赶上(TimeoutException异常){}
}
}
公共静态字符串SetPortName(字符串defaultPortName)
{
串PORTNAME;
PORTNAME =COM6;
返回PORTNAME;
}
公共静态INT SetPortBaudRate(INT defaultPortBaudRate)
{
串波特率;
波特率=9600;
返回int.Parse(波特率);
}
公共静态奇偶SetPortParity(奇偶defaultPortParity)
{
串奇偶校验;
平价=无;
回报(奇偶校验)Enum.Parse(typeof运算(奇偶校验位),奇偶校验);
}
公共静态INT SetPortDataBits(INT defaultPortDataBits)
{
串数据位;
DATABITS =8;
返回int.Parse(数据位);
}
公共静态停止位SetPortStopBits(停止位defaultPortStopBits)
{
线停止位;
STOPBITS =一;
回报(停止位)Enum.Parse(typeof运算(停止位),停止位);
}
公共静态握手SetPortHandshake(握手defaultPortHandshake)
{
串握手;
握手=无;
回报(握手)Enum.Parse(typeof运算(握手),握手);
}
}
解决方案
数据从端口将二进制(字节)总是,因此它取决于如何解释数据。假设字节是ASCII,您可以在如下编码为一个字符串:
字节[] binaryData; //假设binaryData包含从端口字节。
ASCII字符串= Encoding.ASCII.GetString(binaryData);
Here is the deal. I found a source code and changed it a little bit so i can retrieve data from a receiver that is on com6. The data i am receiving is binary. What i want is to convert it to a string so i can cut parts of the string and decode them seperately. how can i dot his? The source code is beneath.
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);
}
}
解决方案
Data from ports will always come in binary(bytes), therefore it depends on how to interpret the data. Assuming that the bytes are ASCII, you can encode it to a string as follows:
byte[] binaryData ; // assuming binaryData contains the bytes from the port.
string ascii = Encoding.ASCII.GetString(binaryData);
这篇关于C#二进制数据转换成字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文