我有一些像這樣的代碼,它將串行端口數據存儲到int命名緩衝區數組中 現在我想讓該緩衝區將其轉換回字符串。我怎樣才能做到這一點?如何將接收到的字節轉換回字符串?
private void serialPort_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
//if (cCommon.DecryptText(CallerId) == "enable")
//{
if (buffer.Length > 0)
{
try
{
for (int c = 0; c != serialPort.BytesToRead; c++)
{
buffer[pointer] = serialPort.ReadByte();
pointer++;
}
}
catch (TimeoutException x)
{
//BackgroundWorker bw = new BackgroundWorker();
bw = new BackgroundWorker();
bw.DoWork += new DoWorkEventHandler(bw_DoWork);
bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
bw.RunWorkerAsync();
}
}
// }
//else
//{
// MessageBox.Show("You do not have permission to use This feature serialPort", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
//}
}
首先爲什麼使用int數組?字節數組不應該更好地服務?你有沒有嘗試搜索一下?例如,這樣的東西可以幫助http://stackoverflow.com/questions/11654562/how-convert-byte-array-to-string或http://stackoverflow.com/questions/1003275/converting-byte-to-string -in-c-sharp –
您在代碼中缺少右括號。 – Max
你爲什麼使用int數組? – Idov