By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,870 Members | 1,212 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,870 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
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.