472,143 Members | 1,578 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

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 3503
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 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.