如何在C#中将字符串转换为字节数组,反之亦然 [英] How to convert string to byte array and vice versa in c#

查看:71
本文介绍了如何在C#中将字符串转换为字节数组,反之亦然的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我通过以下方法将Byte数组转换为String:
我的Byte数组是myArray,我从另一个地方获取它,
所以,我不知道用来获取它的代码页

Hi guys,

I convert an array of Byte to String by this method:
my array of Byte is myArray that i get it form another place,
So, i do not know the code page that used to get it

StringBuilder StringPlus = new StringBuilder();
for (int i = 0; i < myArray.Length; i++)
{
    StringPlus.Append(myArray[i].ToString());
}



我想从String转换为Byte,而结果数组等于我的第一个
重要说明:
我不知道用于获取原始数组 myArray
的代码页


我想要的是:
从字节数组转换为字符串,然后再次转换为字节数组
不是从字符串到字节数组,而是从字符串



I want to convert from String to Byte whereas the resulted Array equal to my first
Important note:
I do not know the Without knowing the code page that used to get the original array myArray


i want that:
convert from Byte array to string then to Byte array again
Not from string to Byte array then to string

推荐答案

由于以下事实,可以用几种不同的方式将字符串转换为字节数组:.NET支持Unicode,并且Unicode标准化了几种称为UTF的差异编码.它们具有不同的字节表示长度,但在某种意义上是等效的,即在对字符串进行编码时,可以将其编码回字符串,但是如果字符串使用一个UTF编码并在假定具有不同的UTF的情况下解码,则可以拧紧.上.

此外,.NET支持非Unicode编码,但在通常情况下无效(仅在实际字符串(例如ASCII)中使用Unicode代码点的有限子集时才有效).在内部,.NET支持UTF-16,但是对于流表示,通常使用UTF-8.它也是Internet的标准事实.

毫不奇怪,类System.Text.Encoding支持字符串将其序列化为字节数组并进行反序列化,该类是一个抽象类.它的派生类支持具体的编码:ASCIIEncoding和四个UTF(System.Text.UnicodeEncoding支持UTF-16).
请参阅 http://msdn.microsoft.com/en-us/library/system.text .encoding.aspx [ ^ ].

对于序列化为字节数组,请使用System.Text.Encoding.GetBytes.对于逆运算,请使用System.Text.Encoding.GetChars.此函数返回字符数组,因此要获取字符串,请使用字符串构造器System.String(char[]).

参见 http://unicode.org/ [ ^ ], http://unicode.org/faq/utf_bom.html [ ^ ].


示例:

String can be converted do byte array in few different ways, due to the following fact: .NET supports Unicode, and Unicode standardizes several difference encodings called UTFs. They have different lengths of byte representation but are equivalent in that sense that when a string is encoded, it can be coded back to the string, but if the string is encoded with one UTF and decoded in assumption of different UTF, if can be screwed up.

Also, .NET supports non-Unicode encodings, but they are not valid in general case (will be valid only if a limited sub-set of Unicode code point is used in an actual string, such as ASCII). Internally, .NET supports UTF-16, but for stream representation UTF-8 is usually used. It is also a standard-de-facto for Internet.

Not surprisingly, serialization of string into array of byte and deserialization is supported by the class System.Text.Encoding, which is an abstract class; its derived classes support concrete encodings: ASCIIEncoding and four UTFs (System.Text.UnicodeEncoding supports UTF-16).

See http://msdn.microsoft.com/en-us/library/system.text.encoding.aspx[^].

For serialization to array of bytes use System.Text.Encoding.GetBytes. For the inverse operation use System.Text.Encoding.GetChars. This function returns array of characters, so to get a string, use a string constructor System.String(char[]).

See http://unicode.org/[^], http://unicode.org/faq/utf_bom.html[^].


Example:

string myString = //... some string

System.Text.Encoding encoding = System.Text.Encoding.UTF8; //or some other, but prefer some UTF is Unicode is used
byte[] bytes = encoding.GetBytes(myString);

//next lines are written in response to a follow-up questions:

myString = new string(encoding.GetChars(bytes));
byte[] bytes = encoding.GetBytes(myString);
myString = new string(encoding.GetChars(bytes));
byte[] bytes = encoding.GetBytes(myString);

//how many times shall I repeat it to show there is a round-trip? :-)



-SA



—SA


//string to byte array
string theMessage = "This is a message!";
byte[] bytes = Encoding.ASCII.GetBytes(theMessage);
//byte array to string
string theMessage2 = Encoding.ASCII.GetString(bytes);
Console.WriteLine(theMessage2);


//string to byte array
String str = "Hello";
byte[] btarr = ASCIIEncoding.ASCII.GetBytes(str);

//byte array to string
StringBuilder StringPlus = new StringBuilder();
for (int i = 0; i < btarr.Length; i++)
{
   StringPlus.Append(Convert.ToChar(btarr[i]));
}


这篇关于如何在C#中将字符串转换为字节数组,反之亦然的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆