Byte arry to text for textbox

  • Thread starter Thread starter tawright915
  • Start date Start date
T

tawright915

I want to convert a byte array to a string or text to write it out in a
multiline textbox. I've tried encoding, and converting but end up with
nothing in the textbox.

The strange thing is that it writes out fine to a text document using
StreamWriter.

So I have this tcp/ip app that receives everything into a byte array.
I then want to write out what it was that was passed to me, into a
multiline textbox.

Any ideas?

Thanks
Tom
 
Tom,

The encoder is the right way to go. It is possible that you are using
it the wrong way.

Can you post the relevant sections of your code?
 
Hello, tawright915!

t> The strange thing is that it writes out fine to a text document using
t> StreamWriter.

t> So I have this tcp/ip app that receives everything into a byte array.
t> I then want to write out what it was that was passed to me, into a
t> multiline textbox.

In text you can see your data in the hex format, if it is okay for you you can convert byte array to string
using "X" format in the ToString(...) method ( .ToString("X") )

--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com
 
Have you tried System.Text.ASCIIEncoding.GetString(System.Byte[])?

smc750
www.certdev.com
I want to convert a byte array to a string or text to write it out in a
multiline textbox. I've tried encoding, and converting but end up with
nothing in the textbox.

The strange thing is that it writes out fine to a text document using
StreamWriter.

So I have this tcp/ip app that receives everything into a byte array.
I then want to write out what it was that was passed to me, into a
multiline textbox.

Any ideas?

Thanks
Tom

--
smc750
www.certdev.com

Message posted via DotNetMonster.com
http://www.dotnetmonster.com/Uwe/Forums.aspx/dotnet-csharp/200605/1
 
Yeah, my code is:
textBox1.Text = Encoding.UTF8.GetString(e.RecvdBuffer, 0,
e.RecvdBufferSize);


Thanks
Tom
 
tawright915 said:
Yeah, my code is:
textBox1.Text = Encoding.UTF8.GetString(e.RecvdBuffer, 0,
e.RecvdBufferSize);

And does the buffer genuinely represent UTF-8 encoded text? If it
doesn't, the above is likely to produce gibberish.

Where did the byte array come from, and how do you actually want to
display it?
 
The buffer is the receive buffer from my socket connection.
The complete code is this:

private void msh_OnMessageRcvd(object sender, ReceivedData e)
{
listBox1.Items.Add("Data Received");

char[] hexDigits = {'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
byte[] bytes = new byte[e.RecvdBufferSize];
bytes = e.RecvdBuffer;
char[] chars = new char[bytes.Length * 2];
for (int i = 0; i < bytes.Length; i++)
{
int b = bytes;
chars[i * 2] = hexDigits[b >> 4];
chars[i * 2 + 1] = hexDigits[b & 0xF];
}
StreamWriter sr = File.CreateText("c:\\myNYSPINdump.txt");
sr.Write(Encoding.UTF8.GetString(e.RecvdBuffer, 0,
e.RecvdBufferSize));
sr.Close();
listBox1.Items.Add(new string(chars));
textBox1.Text = Encoding.UTF8.GetString(e.RecvdBuffer, 0,
e.RecvdBufferSize);
//listBox1.Items.Add("Raw Message: " +
Encoding.ASCII.GetString(e.RecvdBuffer, 26, e.RecvdBufferSize - 4));
}


Basically I'm converting the buffer first to a char to put into a
listbox (Which I plan on removing). Then I write it to a text file.
(this too is going I just had this here to see what was coming in)
Then I'm trying to write it out to a multiline textbox.
 
Good question...I need to check. However it should show the correct
size as the list box and the streamwriter is writing out the correct
string.
 
tawright915 said:
The buffer is the receive buffer from my socket connection.
The complete code is this:

<snip>

Well, that doesn't really tell us what the content of the data is. If
it's arbitrary binary data (which it looks like you're expecting it to
be, given that you're writing it to a text file as hex) why not just
display it as hex too?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top