c#中如何将二进制转化为字符串

2025-02-23 23:34:29
推荐回答(3个)
回答1:

你这个是,读取一个二进制的文件?


把二进制的文件内容,转成string类型的内容?

using (FileStream stream = File.OpenRead(@"路径"))
{
    byte[] content = new byte[stream.Length];

    for (int i = 0; i < content.Length; i++)
    {
        content[i] = (byte)stream.ReadByte();
    }

    Console.WriteLine(Encoding.Default.GetString(content));
}

回答2:

public string ByteToString(byte[] inputBytes)
{
    StringBuilder temp = new StringBuilder(2048);
    foreach (byte tempByte in inputBytes)
    {
        temp.Append(tempByte > 15 ?
        Convert.ToString(tempByte, 2) : '0' + Convert.ToString(tempByte, 2));
    }
    return temp.ToString();
}

回答3:

用convert.tostring方法```