469,632 Members | 1,681 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

how to convert a char array(byte[]) to a string variable?

Hi,

how to convert a char array(byte[]) to a string variable?

byte[] buffer;
string strTest;

/*the blow codes all get type of 'buffer': System.Byte[]!
strTest = buffer.ToString()
strTest = System.Convert.ToString(buffer)
*/

Thanks.
Mar 16 '06 #1
3 3390
Depends on how your string is encoded in the byte[], is it UTF8, ASCII...?
Use the classes derived from the System.Text.Encoding class such as
System.Text.UTF8Encoding.

class Program {
static void Main() {
System.Text.Encoding utf = new System.Text.UTF8Encoding();
string str = "hello";
byte[] bin = utf.GetBytes(str);
string strCopy = utf.GetString(bin);
}
}

Patrick Smacchia
MVP.NET
Author of Practical .NET2 and C#2 http://www.PracticalDOT.NET
Author of NDepend: http://www.NDepend.com


"David" <Da*************@hotmail.com> a écrit dans le message de news:
e%****************@TK2MSFTNGP12.phx.gbl...
Hi,

how to convert a char array(byte[]) to a string variable?

byte[] buffer;
string strTest;

/*the blow codes all get type of 'buffer': System.Byte[]!
strTest = buffer.ToString()
strTest = System.Convert.ToString(buffer)
*/

Thanks.

Mar 16 '06 #2
David <Da*************@hotmail.com> wrote:
how to convert a char array(byte[]) to a string variable?

byte[] buffer;
string strTest;

/*the blow codes all get type of 'buffer': System.Byte[]!
strTest = buffer.ToString()
strTest = System.Convert.ToString(buffer)
*/


Aside from Patrick's answer, your first sentence reveals a
misunderstanding. A char array is *not* a byte[]. A byte[] is a byte
array, and a char[] is a char array. Chars are 16 bits in C#, always.

--
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
Mar 17 '06 #3
Thank you. I see.

"David" <Da*************@hotmail.com> дÈëÓʼþ
news:e%****************@TK2MSFTNGP12.phx.gbl...
Hi,

how to convert a char array(byte[]) to a string variable?

byte[] buffer;
string strTest;

/*the blow codes all get type of 'buffer': System.Byte[]!
strTest = buffer.ToString()
strTest = System.Convert.ToString(buffer)
*/

Thanks.

Mar 17 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by techno | last post: by
5 posts views Thread by Terry Olsen | last post: by
4 posts views Thread by John Smith | last post: by
4 posts views Thread by msosno01 | last post: by
5 posts views Thread by 9966 | last post: by
10 posts views Thread by =?Utf-8?B?Um95?= | last post: by
6 posts views Thread by Bob Altman | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.