PC Review


Reply
Thread Tools Rate Thread

convert byte[] to string and way around

 
 
=?Utf-8?B?V2lsZnJpZWQgTWVzdGRhZ2g=?=
Guest
Posts: n/a
 
      21st Mar 2005
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
 
Reply With Quote
 
 
 
 
Jeti [work]
Guest
Posts: n/a
 
      21st Mar 2005
> 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"




--
(E-Mail Removed)
http://www.programeri.org
http://forum.programeri.org


 
Reply With Quote
 
Denny Britz
Guest
Posts: n/a
 
      21st Mar 2005
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 ?
>

 
Reply With Quote
 
Cor Ligthert
Guest
Posts: n/a
 
      21st Mar 2005
Wilfried,

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

I hope this helps,

Cor


 
Reply With Quote
 
=?Utf-8?B?V2lsZnJpZWQgTWVzdGRhZ2g=?=
Guest
Posts: n/a
 
      21st Mar 2005
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.
 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      22nd Mar 2005
Wilfried Mestdagh <(E-Mail Removed)> 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 - <(E-Mail Removed)>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
=?Utf-8?B?V2lsZnJpZWQgTWVzdGRhZ2g=?=
Guest
Posts: n/a
 
      22nd Mar 2005
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
 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      22nd Mar 2005
Wilfried Mestdagh <(E-Mail Removed)> 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 - <(E-Mail Removed)>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Convert Byte() <-> String Bob Altman Microsoft VB .NET 6 24th Jun 2008 03:47 AM
How to convert a String into Byte[] EOS Microsoft C# .NET 5 5th Dec 2005 05:46 AM
How to convert a byte() to string Dean Slindee Microsoft VB .NET 4 17th Oct 2005 06:00 PM
How to convert byte() to string, and from string back to byte() moondaddy Microsoft VB .NET 4 15th Aug 2005 03:25 PM
How Do I: convert a String to Byte[] Russell Mangel Microsoft Dot NET 3 14th Dec 2003 03:51 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:39 AM.