如何将字节数组转换为字符串

时间:2022-10-30 11:52:12

I have a byte array which I got back from a FileStream.Read and I would like to turn that into a string. I'm not 100% sure of the encoding - it's just a file i saved to disk - how do I do the conversion? Is there a .NET class that reads the byte order mark and can figure out the encoding for me?

我有一个字节数组,我从FileStream.Read返回,我想把它变成一个字符串。我不是100%肯定编码 - 它只是我保存到磁盘的文件 - 我该如何进行转换?是否有一个.NET类读取字节顺序标记并可以为我找出编码?

6 个解决方案

#1


If File.ReadAllText will read the file correctly, then you have a couple of options.

如果File.ReadAllText将正确读取文件,那么您有几个选项。

Instead of calling BeginRead, you could just call File.ReadAllText asynchronously:

您可以只异步调用File.ReadAllText,而不是调用BeginRead:

    delegate string AsyncMethodCaller(string fname);

    static void Main(string[] args)
    {
        string InputFilename = "testo.txt";
        AsyncMethodCaller caller = File.ReadAllText;
        IAsyncResult rslt = caller.BeginInvoke(InputFilename, null, null);

        // do other work ...

        string fileContents = caller.EndInvoke(rslt);
    }

Or you can create a MemoryStream from the byte array, and then use a StreamReader on that.

或者您可以从字节数组创建一个MemoryStream,然后使用StreamReader。

#2


See how-to-guess-the-encoding-of-a-file-with-no-bom-in-net.

Since strings are Unicode, you must specify an encoding on conversion. Text streams (even ReadAllText() ) have an active encoding inside, usually some sensible default.

由于字符串是Unicode,因此必须在转换时指定编码。文本流(甚至是ReadAllText())内部都有一个有效的编码,通常是一些合理的默认值。

#3


Try something like this:

尝试这样的事情:

buffer = Encoding.Convert( Encoding.GetEncoding("iso-8859-1"), Encoding.UTF8, buffer );
newString = Encoding.UTF8.GetString( buffer, 0, len );

#4


How much do you know about the file? Could it really be any encoding? If so, you'd need to use heuristics to guess the encoding. If it's going to be UTF-8, UTF-16 or UTF-32 then

你对这个档案了解多少?真的可以是任何编码吗?如果是这样,您需要使用启发式方法来猜测编码。如果它将是UTF-8,UTF-16或UTF-32那么

new StreamReader(new MemoryStream(bytes), true)

will detect the encoding for you automatically. Text is pretty nasty if you really don't know the encoding though. There are plenty of cases where you really would just be guessing.

将自动检测您的编码。如果你真的不知道编码,那么文本是非常讨厌的。有很多情况下你真的会猜测。

#5


There is no simple way to get the encoding, but as mentioned above use

没有简单的方法来获得编码,但如上所述使用

string str = System.Text.Encoding.Default.GetString(mybytearray);

if you have no clue of what the encoding is. If you are in europe the ISO-8859-1 is probably the encoding you have.

如果您不知道编码是什么。如果您在欧洲,ISO-8859-1可能是您的编码。

string str = System.Text.Encoding.GetEncoding("ISO-8859-1").GetString(mybytearray);

#6


System.IO.File.ReadAllText does what you want.

System.IO.File.ReadAllText做你想要的。

#1


If File.ReadAllText will read the file correctly, then you have a couple of options.

如果File.ReadAllText将正确读取文件,那么您有几个选项。

Instead of calling BeginRead, you could just call File.ReadAllText asynchronously:

您可以只异步调用File.ReadAllText,而不是调用BeginRead:

    delegate string AsyncMethodCaller(string fname);

    static void Main(string[] args)
    {
        string InputFilename = "testo.txt";
        AsyncMethodCaller caller = File.ReadAllText;
        IAsyncResult rslt = caller.BeginInvoke(InputFilename, null, null);

        // do other work ...

        string fileContents = caller.EndInvoke(rslt);
    }

Or you can create a MemoryStream from the byte array, and then use a StreamReader on that.

或者您可以从字节数组创建一个MemoryStream,然后使用StreamReader。

#2


See how-to-guess-the-encoding-of-a-file-with-no-bom-in-net.

Since strings are Unicode, you must specify an encoding on conversion. Text streams (even ReadAllText() ) have an active encoding inside, usually some sensible default.

由于字符串是Unicode,因此必须在转换时指定编码。文本流(甚至是ReadAllText())内部都有一个有效的编码,通常是一些合理的默认值。

#3


Try something like this:

尝试这样的事情:

buffer = Encoding.Convert( Encoding.GetEncoding("iso-8859-1"), Encoding.UTF8, buffer );
newString = Encoding.UTF8.GetString( buffer, 0, len );

#4


How much do you know about the file? Could it really be any encoding? If so, you'd need to use heuristics to guess the encoding. If it's going to be UTF-8, UTF-16 or UTF-32 then

你对这个档案了解多少?真的可以是任何编码吗?如果是这样,您需要使用启发式方法来猜测编码。如果它将是UTF-8,UTF-16或UTF-32那么

new StreamReader(new MemoryStream(bytes), true)

will detect the encoding for you automatically. Text is pretty nasty if you really don't know the encoding though. There are plenty of cases where you really would just be guessing.

将自动检测您的编码。如果你真的不知道编码,那么文本是非常讨厌的。有很多情况下你真的会猜测。

#5


There is no simple way to get the encoding, but as mentioned above use

没有简单的方法来获得编码,但如上所述使用

string str = System.Text.Encoding.Default.GetString(mybytearray);

if you have no clue of what the encoding is. If you are in europe the ISO-8859-1 is probably the encoding you have.

如果您不知道编码是什么。如果您在欧洲,ISO-8859-1可能是您的编码。

string str = System.Text.Encoding.GetEncoding("ISO-8859-1").GetString(mybytearray);

#6


System.IO.File.ReadAllText does what you want.

System.IO.File.ReadAllText做你想要的。