如何把System.String对象与Byte Array互相转换?

String转换为Byte Array

// C# to convert a string to a byte array.
public static byte[] StrToByteArray(string str)
{
    System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
    return encoding.GetBytes(str);
}

 

Byte Array转换为String

// C# to convert a byte array to a string.
public static byte[] ByteArrayToStr(string str)
{
    byte[] dBytes = { 1, 2, 3 };
    string string1;
    System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
    string1 = enc.GetString(dBytes);
}

 

资料来源:

How do I convert a string to a byte array and vica-versa in VB.NET and C#?

http://www.chilkatsoft.com/faq/dotnetstrtobytes.html

posted on 2010-03-11 14:22  中道学友  阅读(1834)  评论(0编辑  收藏  举报

导航

技术追求准确,态度积极向上