Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 字节[]到ASCII_C#_.net_File Upload_Bitconverter - Fatal编程技术网

C# 字节[]到ASCII

C# 字节[]到ASCII,c#,.net,file-upload,bitconverter,C#,.net,File Upload,Bitconverter,我收到了以二进制值返回的文本文件的内容: Byte[] buf = new Byte[size]; stream = File.InputStream; stream.Read(buf, 0, size); 如何将其转换为ASCII?使用: 作为将数据从流读取到字节数组的替代方法,您可以让框架处理所有事情,只需使用带有ASCII编码的设置来读取字符串。这样,您就不必担心获得合适的缓冲区大小或更大的数据大小 using (var reader = new StreamReader(stream,

我收到了以二进制值返回的文本文件的内容:

Byte[] buf = new Byte[size];
stream = File.InputStream;
stream.Read(buf, 0, size);
如何将其转换为ASCII?

使用:


作为将数据从流读取到字节数组的替代方法,您可以让框架处理所有事情,只需使用带有ASCII编码的设置来读取字符串。这样,您就不必担心获得合适的缓冲区大小或更大的数据大小

using (var reader = new StreamReader(stream, Encoding.ASCII))
{
    string theString = reader.ReadToEnd();
    // do something with theString
}
您可以使用:

System.Text.Encoding.ASCII.GetString(buf);
但有时你会得到一个奇怪的数字,而不是你想要的字符串。在这种情况下,当您看到原始字符串时,它可能有一些十六进制字符。如果是这种情况,您可能希望尝试以下方法:

System.Text.Encoding.UTF8.GetString(buf);
或者作为最后手段:

System.Text.Encoding.Default.GetString(bytearray);
将字节转换为字符串

在派生类中重写时,将指定字节数组中的所有字节解码为字符串

名称空间:System.Text
程序集:mscorlib(在mscorlib.dll中)

语法

public virtual string GetString(byte[] bytes)
参数

bytes
    Type: System.Byte[]
    The byte array containing the sequence of bytes to decode.
ArgumentException        - The byte array contains invalid Unicode code points.
ArgumentNullException    - bytes is null.
DecoderFallbackException - A fallback occurred (see Character Encoding in the .NET Framework for complete explanation) or DecoderFallback is set to DecoderExceptionFallback.
返回值

类型:System.String
包含对指定字节序列进行解码的结果的字符串

例外情况

bytes
    Type: System.Byte[]
    The byte array containing the sequence of bytes to decode.
ArgumentException        - The byte array contains invalid Unicode code points.
ArgumentNullException    - bytes is null.
DecoderFallbackException - A fallback occurred (see Character Encoding in the .NET Framework for complete explanation) or DecoderFallback is set to DecoderExceptionFallback.
备注

bytes
    Type: System.Byte[]
    The byte array containing the sequence of bytes to decode.
ArgumentException        - The byte array contains invalid Unicode code points.
ArgumentNullException    - bytes is null.
DecoderFallbackException - A fallback occurred (see Character Encoding in the .NET Framework for complete explanation) or DecoderFallback is set to DecoderExceptionFallback.
如果要转换的数据是 仅在顺序块中可用 (如从流中读取的数据)或 如果数据量太大以至于 它需要被分成更小的部分 块,应用程序应使用 由制造商提供的解码器或编码器 GetDecoder方法或GetEncoder 方法,分别导出 班级

见下面的备注 Encoding.GetChars以进行更多讨论 解码技术的发展与应用 考虑因素

成功了!它也适用于输入缓冲区的任何子集-使用带有两个额外参数的变量:(或在缓冲区结束之前不使用第三个参数)。(您可以在答案中包含此信息,以获得更全面的答案,尽管没有明确要求。)