469,280 Members | 1,855 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,280 developers. It's quick & easy.

convert byte[] to string and way around

Hi,

how to typecast or convert a byte[] to a string and the way arount ?
I have string from a textbox and wants to transmit it trough a socket. witch
accepts a byte[]. Now I make a copy from the string first, but I'm sure there
is some other way. For receiving I have to display the received string into
a textbox. same question ?

--
rgds, Wilfried
http://www.mestdagh.biz
Nov 16 '05 #1
7 60608
> how to typecast or convert a byte[] to a string and the way arount ?
I have string from a textbox and wants to transmit it trough a socket. witch accepts a byte[]. Now I make a copy from the string first, but I'm sure there is some other way. For receiving I have to display the received string into a textbox. same question ?

Check out this class: "BitConverter"


--
Je**@programeri.org
http://www.programeri.org
http://forum.programeri.org
Nov 16 '05 #2
Hi,

string s = System.Text.Encoding.UTF8.GetString(bytes);
byte[] bytes = System.Text.Encoding.UTF8.GetBytes(s);

Hi,

how to typecast or convert a byte[] to a string and the way arount ?
I have string from a textbox and wants to transmit it trough a socket. witch
accepts a byte[]. Now I make a copy from the string first, but I'm sure there
is some other way. For receiving I have to display the received string into
a textbox. same question ?

Nov 16 '05 #3
Wilfried,

Normally is this the class for what you ask.
http://msdn.microsoft.com/library/de...classtopic.asp

I hope this helps,

Cor
Nov 16 '05 #4
Hi,

thank it is workinbut followig proble ifr receiving data:

private void DoDataAvailable(byte[] buffer, int offset, int count)

how to convert this to string ? offset is offset begin of string, and count
is amountof data.
Nov 16 '05 #5
Wilfried Mestdagh <Wi**************@discussions.microsoft.com> wrote:
thank it is workinbut followig proble ifr receiving data:

private void DoDataAvailable(byte[] buffer, int offset, int count)

how to convert this to string ? offset is offset begin of string, and count
is amountof data.


Denny already showed you the code - use Encoding.GetString.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #6
Hi Jon,

Sorr I was not clear in this question. I have a pointer to a buffer with
offset in ti where data start, and count of data. To convert to string now I
need to make a double copy whitch should not be. I show because it will
explain better:

private void DoDataAvailable(byte[] buffer, int offset, int count)
{
count -= 2; // remove \r\n
byte[] tmp = new byte[count];
while (--count >= 0)
tmp[count] = buffer[offset + count];
string rx = Encoding.UTF8.GetString(tmp);

I wanted to avoid the extra copy of the data in tmp here.

thx, Wilfried
Nov 16 '05 #7
Wilfried Mestdagh <Wi**************@discussions.microsoft.com> wrote:
Sorr I was not clear in this question. I have a pointer to a buffer with
offset in ti where data start, and count of data. To convert to string now I
need to make a double copy whitch should not be. I show because it will
explain better:

private void DoDataAvailable(byte[] buffer, int offset, int count)
{
count -= 2; // remove \r\n
byte[] tmp = new byte[count];
while (--count >= 0)
tmp[count] = buffer[offset + count];
string rx = Encoding.UTF8.GetString(tmp);

I wanted to avoid the extra copy of the data in tmp here.


Use the overload of Encoding.GetString which takes a start index and a
count.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Brian | last post: by
4 posts views Thread by John Smith | last post: by
4 posts views Thread by Sam | last post: by
5 posts views Thread by EOS | last post: by
15 posts views Thread by Steve | last post: by
6 posts views Thread by Bob Altman | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.