By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,204 Members | 1,469 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,204 IT Pros & Developers. It's quick & easy.

Byte arry to text for textbox

P: n/a
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

May 10 '06 #1
Share this Question
Share on Google+
9 Replies


P: n/a
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?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"tawright915" <ta*********@yahoo.com> wrote in message
news:11**********************@j33g2000cwa.googlegr oups.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

May 10 '06 #2

P: n/a
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
May 10 '06 #3

P: n/a
Have you tried System.Text.ASCIIEncoding.GetString(System.Byte[])?

smc750
www.certdev.com

tawright915 wrote:
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/For...sharp/200605/1
May 10 '06 #4

P: n/a
Yeah, my code is:
textBox1.Text = Encoding.UTF8.GetString(e.RecvdBuffer, 0,
e.RecvdBufferSize);
Thanks
Tom

May 11 '06 #5

P: n/a
tawright915 <ta*********@yahoo.com> wrote:
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?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
May 11 '06 #6

P: n/a
What does the value of e.RecvdBufferSize show? What type is e?

May 11 '06 #7

P: n/a
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[i];
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.

May 11 '06 #8

P: n/a
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.

May 11 '06 #9

P: n/a
tawright915 <ta*********@yahoo.com> wrote:
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?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
May 12 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.