Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

convert byte[] to string?

0 views
Skip to first unread message

George Ter-Saakov

unread,
Jun 23, 2003, 9:54:54 AM6/23/03
to
I have a Stream but at some point I need to convert byte array to string?
What is most efficient way to do that?

public override void Write(byte[] buffer, int offset, int count)

{

String sKey = new String((sbyte[])buffer,offset, count); -- does not
compile. Can not convert byte[] to sbyte[].

}

Thanks.

George.


Ignacio Machin

unread,
Jun 23, 2003, 10:04:24 AM6/23/03
to
Hi George,

Did you tried BitConverter.ToString( byte[] ) ?

Hope this help,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"George Ter-Saakov" <w...@hotmail.com> wrote in message
news:%23R$NI8YOD...@TK2MSFTNGP11.phx.gbl...

R Agam

unread,
Jun 23, 2003, 10:03:47 AM6/23/03
to
string s = Use System.Text.Encoding.ASCII.GetString (buffer);

Ronen

"George Ter-Saakov" <w...@hotmail.com> wrote in message

news:#R$NI8YOD...@TK2MSFTNGP11.phx.gbl...

Jon Skeet

unread,
Jun 23, 2003, 10:05:15 AM6/23/03
to
George Ter-Saakov <w...@hotmail.com> wrote:
> I have a Stream but at some point I need to convert byte array to string?
> What is most efficient way to do that?

You should use Encoding.GetString (byte[]) - but which encoding you
wish to use depends on the application. Is the data fundamentally text
data? If not, I would suggest not converting it into a string at all,
or using something like Base16 or Base64. If it is, you will hopefully
know the encoding.

See http://www.pobox.com/~skeet/csharp/unicode.html for more
information.

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

Ignacio Machin

unread,
Jun 23, 2003, 10:11:48 AM6/23/03
to
Sorry, Wrong answer

You can use ASCIIEncoding.GetString( byte[] ) for that.

Sorry for the first post

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Ignacio Machin" <ignacio.machin AT dot.state.fl.us> wrote in message
news:OAi6cBZO...@TK2MSFTNGP11.phx.gbl...

Chris Capel

unread,
Jun 23, 2003, 10:15:24 AM6/23/03
to
This one's easy. Just use a StreamReader.

System.IO.MemoryStream s = new MemoryStream(buffer, offset, count);
System.IO.StreamReader read = new StreamReader(s);
stsring sKey = read.ReadToEnd();

Hope this works for you.

Chris

"George Ter-Saakov" <w...@hotmail.com> wrote in message

news:#R$NI8YOD...@TK2MSFTNGP11.phx.gbl...

Jon Skeet

unread,
Jun 23, 2003, 10:23:37 AM6/23/03
to
"Ignacio Machin" <ignacio.machin AT dot.state.fl.us> <"Ignacio Machin"
<ignacio.machin AT dot.state.fl.us>> wrote:
> Sorry, Wrong answer
>
> You can use ASCIIEncoding.GetString( byte[] ) for that.

Well, that's just as wrong if the data isn't originally in ASCII :)

It really depends what the data really *is*...

George Ter-Saakov

unread,
Jun 23, 2003, 10:37:05 AM6/23/03
to
Thanks to everyone.
I am writing a filter for HttpResponse so my guess I should use
System.Text.Encoding.UTF8

And I can not use any other format since that filter looks for tokens (given
as strings) and substitutes them with special phrases. So somehow I need to
convert bytes back to the string to match it against tokens.

Thanks.
George.

"Jon Skeet" <sk...@pobox.com> wrote in message
news:MPG.196119714...@news.microsoft.com...

Jon Skeet

unread,
Jun 23, 2003, 10:41:13 AM6/23/03
to
Chris Capel <as...@asdf.com> wrote:
> This one's easy. Just use a StreamReader.
>
> System.IO.MemoryStream s = new MemoryStream(buffer, offset, count);
> System.IO.StreamReader read = new StreamReader(s);
> stsring sKey = read.ReadToEnd();

Yikes, that seems a longwinded way of doing:

string sKey = Encoding.UTF8.GetString (buffer, offset, count);

:)

gerry

unread,
Jun 23, 2003, 1:22:11 PM6/23/03
to
wow.


"Chris Capel" <as...@asdf.com> wrote in message
news:etzk7HZO...@TK2MSFTNGP10.phx.gbl...

Chris Capel

unread,
Jun 23, 2003, 5:47:06 PM6/23/03
to
Hey, what can I say? I'd never heard of the System.Text.AsciiEncoding class
before.

Chris

"gerry" <ge...@hotmail.com> wrote in message
news:es5asyaO...@tk2msftngp13.phx.gbl...

Joerg Jooss

unread,
Jun 29, 2003, 9:41:45 AM6/29/03
to
"George Ter-Saakov" spoke:

> Thanks to everyone.
> I am writing a filter for HttpResponse so my guess I should use
> System.Text.Encoding.UTF8

Well, don't guess -- check it out ;-)

Encoding encoding = Response.ContentEncoding;

Cheers,
--
Joerg Jooss
joerg...@gmx.net

0 new messages