473,386 Members | 2,114 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Byte arry to text for textbox

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
9 10975
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
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
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
Yeah, my code is:
textBox1.Text = Encoding.UTF8.GetString(e.RecvdBuffer, 0,
e.RecvdBufferSize);
Thanks
Tom

May 11 '06 #5
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
What does the value of e.RecvdBufferSize show? What type is e?

May 11 '06 #7
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: kuhni | last post by:
Hi everybody! After searching newsgroups for a couple of hours, I now try asking directly (running the risk of asking again the same question). My problem is to predict when the size of the...
0
by: Earl Teigrob | last post by:
I can create a new custom control (and not change it) and add it to the toolbox and drag it onto the disign screen and it works just fine, displaying the text . However, when I add the following...
1
by: John Dalberg | last post by:
I have a datagrid plus a button to save the contents of the grid to the database. My code gets blanks in the textbox field. I am not sure why. Also I don't know why dgi.Cells.Controls &...
12
by: Mark Rae | last post by:
Hi, Can anyone please tell me how to convert an unserializeable object say, a System.Web.Mail.MailMessage object, to a byte array and then convert the byte array to a Base64 string? Any...
1
by: Emilio | last post by:
Question about Dim data As () = System.Text.Encoding.ASCII.GetBytes(message) Would it be the same to write: Dim data() As Byte = System.Text.Encoding.ASCII.GetBytes(message) ?
4
by: Neil | last post by:
Just found out that the Microsoft Rich Textbox does not support full text justification, since it's based on Version 1.0 of the RichEdit Window Class, and full text justification is only available...
2
by: krishnasamy | last post by:
Hi, I have invoking the "NewDll.Dll" which created using vc++. In this Dll a method return the array data following way, BYTE NewDemoDll::RetLiveImg() { return bufferData; } How can I...
1
by: daonho | last post by:
I tried to use javascript to trigger up the button click function when user press enter key from the textbox. This function work fine with a single button click such has login page. However, if the...
5
by: =?Utf-8?B?RmFtaWx5IFRyZWUgTWlrZQ==?= | last post by:
I'm sorry, but I've read your code a couple of times and just don't see where the Form1 is initialized. Form1 also sounds like a class name, and this would be how you could do some form operations...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.