472,093 Members | 2,486 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

How to convert Byte[] into String?

EOS
Hi,

I would like to ask on how to convert array of bytes, Byte[] into String?

I tried StringIwant = System.Convert.ToString(byteData);

but it actually return "System.Byte[]" rather than convert the data I wanted
into string.

I guess it should be a rather simple question.

By the way, I am using compact framework for PDA.

Thanks in advance. : )
Dec 2 '05 #1
7 57207
You actually want:

string StringIWant = BitConverter.ToString(byteData);

--
Truth,
James Curran
[erstwhile VC++ MVP]

Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
"EOS" <no****@nospam.org> wrote in message
news:ek**************@TK2MSFTNGP12.phx.gbl...
Hi,

I would like to ask on how to convert array of bytes, Byte[] into String?

I tried StringIwant = System.Convert.ToString(byteData);

but it actually return "System.Byte[]" rather than convert the data I wanted into string.

I guess it should be a rather simple question.

By the way, I am using compact framework for PDA.

Thanks in advance. : )

Dec 2 '05 #2
Hi,

It's very simple, you have to use one of the encoding classes provided in
the framework. You see, depending of how you generated the bytes[] from the
original string you get a different set of data, UNICODE are stored as two
bytes, where ASCII are encoded as 1 byte.

Once you know this you use osmething like ASCIIEncoding.GetString( byte[] );

cheers,

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

"EOS" <no****@nospam.org> wrote in message
news:ek**************@TK2MSFTNGP12.phx.gbl...
Hi,

I would like to ask on how to convert array of bytes, Byte[] into String?

I tried StringIwant = System.Convert.ToString(byteData);

but it actually return "System.Byte[]" rather than convert the data I
wanted into string.

I guess it should be a rather simple question.

By the way, I am using compact framework for PDA.

Thanks in advance. : )

Dec 2 '05 #3
If you need something more complex that takes into account different
encoding such as ascii vs. unicode, then use something like
Encoding.GetString(...).

"James Curran" <ja*********@mvps.org> wrote in message
news:ew**************@TK2MSFTNGP14.phx.gbl...
You actually want:

string StringIWant = BitConverter.ToString(byteData);

--
Truth,
James Curran
[erstwhile VC++ MVP]

Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
"EOS" <no****@nospam.org> wrote in message
news:ek**************@TK2MSFTNGP12.phx.gbl...
Hi,

I would like to ask on how to convert array of bytes, Byte[] into String?

I tried StringIwant = System.Convert.ToString(byteData);

but it actually return "System.Byte[]" rather than convert the data I

wanted
into string.

I guess it should be a rather simple question.

By the way, I am using compact framework for PDA.

Thanks in advance. : )


Dec 2 '05 #4
EOS
Actually what I wanted is rather simple.

In C/C++, it is;

byte byteData[500] = .....

CString szMyString = (CString) byteData

It is that simple and straight forward because what is inside the byte array
is exactly the same char[]. Even a simple memcpy will work.

Any ideas on how to implement these simple object convertion in C#?

By the way, I am develope on Compact .NET which is on PDA.

"EOS" <no****@nospam.org> wrote in message
news:ek**************@TK2MSFTNGP12.phx.gbl...
Hi,

I would like to ask on how to convert array of bytes, Byte[] into String?

I tried StringIwant = System.Convert.ToString(byteData);

but it actually return "System.Byte[]" rather than convert the data I
wanted into string.

I guess it should be a rather simple question.

By the way, I am using compact framework for PDA.

Thanks in advance. : )

Dec 2 '05 #5
Hi,

It's as simple in C#:

byte byteData[500] = ...

string s = Encoding..ASCII.GetString( byteData);

cheers,

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

"EOS" <no****@nospam.org> wrote in message
news:ut**************@TK2MSFTNGP14.phx.gbl...
Actually what I wanted is rather simple.

In C/C++, it is;

byte byteData[500] = .....

CString szMyString = (CString) byteData

It is that simple and straight forward because what is inside the byte
array is exactly the same char[]. Even a simple memcpy will work.

Any ideas on how to implement these simple object convertion in C#?

By the way, I am develope on Compact .NET which is on PDA.

"EOS" <no****@nospam.org> wrote in message
news:ek**************@TK2MSFTNGP12.phx.gbl...
Hi,

I would like to ask on how to convert array of bytes, Byte[] into String?

I tried StringIwant = System.Convert.ToString(byteData);

but it actually return "System.Byte[]" rather than convert the data I
wanted into string.

I guess it should be a rather simple question.

By the way, I am using compact framework for PDA.

Thanks in advance. : )


Dec 2 '05 #6
EOS
Wow... Thanks.. and it works!

"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us> wrote
in message news:OA*************@TK2MSFTNGP11.phx.gbl...
Hi,

It's as simple in C#:

byte byteData[500] = ...

string s = Encoding..ASCII.GetString( byteData);

cheers,

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

"EOS" <no****@nospam.org> wrote in message
news:ut**************@TK2MSFTNGP14.phx.gbl...
Actually what I wanted is rather simple.

In C/C++, it is;

byte byteData[500] = .....

CString szMyString = (CString) byteData

It is that simple and straight forward because what is inside the byte
array is exactly the same char[]. Even a simple memcpy will work.

Any ideas on how to implement these simple object convertion in C#?

By the way, I am develope on Compact .NET which is on PDA.

"EOS" <no****@nospam.org> wrote in message
news:ek**************@TK2MSFTNGP12.phx.gbl...
Hi,

I would like to ask on how to convert array of bytes, Byte[] into
String?

I tried StringIwant = System.Convert.ToString(byteData);

but it actually return "System.Byte[]" rather than convert the data I
wanted into string.

I guess it should be a rather simple question.

By the way, I am using compact framework for PDA.

Thanks in advance. : )



Dec 2 '05 #7
EOS <no****@nospam.org> wrote:
Actually what I wanted is rather simple.

In C/C++, it is;

byte byteData[500] = .....

CString szMyString = (CString) byteData

It is that simple and straight forward because what is inside the byte array
is exactly the same char[]. Even a simple memcpy will work.


Hmm... that sounds unlikely, as a byte array has one byte per entry,
but a char array has two bytes per entry in .NET.

It's all a case of working out what encoding you're using. I suspect
that Encoding.Default.GetString(data) is what you want (or possibly the
overload which takes an index and size, if your byte array is longer
than the "real" data in it; you don't want to include the null
terminator, for instance).

--
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
Dec 2 '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
7 posts views Thread by Wilfried Mestdagh | 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
reply views Thread by leo001 | last post: by

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.