如何将LF和Cr添加到char数组中 [英] how to add LF and Cr to array of char

查看:124
本文介绍了如何将LF和Cr添加到char数组中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从串口(sp)收到三个字符,它们是''abc'',我通过使用下面的代码收到它们,其中sp是串口。

I recieve three character from serial port (sp) they are ''abc'', and I received them by using the code below, where sp is the serialport.

MySrting = sp.ReadExisting();
textBox1.AppendText(MySrting);



该字符不可读,它们看起来像是方角。发送三个字符的设备没有添加换行和CR,我的意思是LF和CR



我想问题是丢失了LF和CR,我不知道知道如何将LF和Cr添加到字符串MyString



编辑:

确定我收到来自Pic微控制器的字符串我想收到abc到我的c#程序然后将该字符串存储到textbox1。但字符串变得越来越长我要清除 sp(serialport)所以我再次获得新字符串这是序列端口事件


the character are not readable, and they looks like the corner of square. The device which sent the three character did not add line feed and CR, i mean LF and CR

I guess the problem is loss of LF and CR,i do not know how to add LF and Cr to the string MyString


ok i receive the string from Pic microcontroller i want to recevied abc to my c# program then store that string to textbox1. but the string becomes longer and longer i wich to clear the sp(serialport) so i get new string again this is the serialport event

void serialPort_DataReceived(object s, SerialDataReceivedEventArgs e) 
{
// str is defined somewhere else 
byte[] data = new byte[sp.BytesToRead]; 
sp.Read(data, 0, data.Length);
// sp.Read(data, 0, 8); 
//MessageBox.Show(data.ToString()); 
str=ByteArrayToString2(data); 
MessageBox.Show(str); 
}

推荐答案

数组具有 immutable 大小,只能在数组初始化时定义宾语。您无法向阵列添加任何内容,您只能创建一个全新的数组,原始的调整大小的副本,然后填写所需的字符:

http://msdn.microsoft.com/en-us/library/bb348051.aspx [ ^ ]。



注意这个操作不利于性能。



然而,我会问你为什么要用它,为什么不用你的字符串代替阵列。 (字符串完全不可变,但您可以使用有效的 string.Format 或使用可变类型 System.Text.StringBuilder 。 )



-SA
Arrays have immutable size which can only be defined at initialization of the array object. You cannot add anything to the array, you can only create a brand new array, a resized copy of the original, and then fill in required characters:
http://msdn.microsoft.com/en-us/library/bb348051.aspx[^].

Note that this operation is bad for performance.

However, I would question why would you ever need it at all, and why won''t you use the string instead of array. (Strings are fully immutable, but you can use effective string.Format or use mutable type System.Text.StringBuilder.)

—SA


为她工作



myString = serialPort1.ReadByte()。ToString();
worked her

myString = serialPort1.ReadByte().ToString();


这篇关于如何将LF和Cr添加到char数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆