Click here to Skip to main content
16,005,120 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi,

I have code in c# that read a byte array from a serial port.
I have to display this byte array data received via serial port, in a text box.
This byte array is needed to edit from the for send back to an application.
Anybody please help me to find a solution.

How to display byte array data in textbox.
Posted
Comments
[no name] 14-Aug-14 9:57am    
http://msdn.microsoft.com/en-us/library/System.Text.Encoding_methods(v=vs.110).aspx

The problem is not fully defined. The bytes can represent text, this is one possible option, and other option would be showing the bytes as separate decimal of hexadecimal strings. Or something else.
First options is shown in Solution 1, which also covers ASCII, but the encoding can be different, depending on some requirements.

The second option may require something like;
C#
byte[] example = new byte { 1, 2, 3, };
System.StringBuilder stringBuilder = new System.StringBuilder();
foreach(byte in example)
    stringBuilder.Append(byte.ToString() + ", "); // a format parameter can be added
yourTextBox = stringBuilder.ToString.Trim(); // not the most efficient, just for simplicity


—SA
 
Share this answer
 
You could do this:

byte[] example = new byte { 0x01, 0x02, 0x03 };
YourTextBox.Text = System.Text.Encoding.UTF8.GetString(example)
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 14-Aug-14 14:54pm    
I voted 4. You could have mentioned that this is not the only possible interpretation of the vague OP's request.
Besides, 1, 2 and 3 don't give any readable text. Could be, say, 65, 66, 67 ("ABC").
Please see my answer, where I credit yours.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900