我有一个字节[]数组,从一个文件加载,我碰巧知道包含UTF-8。

在一些调试代码中,我需要将其转换为字符串。是否有一个单行程序可以做到这一点?

在表面之下,它应该只是一个分配和一个memcopy,所以即使没有实现,也应该是可能的。


当前回答

应用(字节)b to弦(“x2”),Outputs b4b5dfe475e58b67

public static class Ext {

    public static string ToHexString(this byte[] hex)
    {
        if (hex == null) return null;
        if (hex.Length == 0) return string.Empty;

        var s = new StringBuilder();
        foreach (byte b in hex) {
            s.Append(b.ToString("x2"));
        }
        return s.ToString();
    }

    public static byte[] ToHexBytes(this string hex)
    {
        if (hex == null) return null;
        if (hex.Length == 0) return new byte[0];

        int l = hex.Length / 2;
        var b = new byte[l];
        for (int i = 0; i < l; ++i) {
            b[i] = Convert.ToByte(hex.Substring(i * 2, 2), 16);
        }
        return b;
    }

    public static bool EqualsTo(this byte[] bytes, byte[] bytesToCompare)
    {
        if (bytes == null && bytesToCompare == null) return true; // ?
        if (bytes == null || bytesToCompare == null) return false;
        if (object.ReferenceEquals(bytes, bytesToCompare)) return true;

        if (bytes.Length != bytesToCompare.Length) return false;

        for (int i = 0; i < bytes.Length; ++i) {
            if (bytes[i] != bytesToCompare[i]) return false;
        }
        return true;
    }

}

其他回答

这是一个不需要编码的结果。我在我的网络类中使用它,并以字符串的形式发送二进制对象。

public static byte[] String2ByteArray(string str)
{
    char[] chars = str.ToArray();
    byte[] bytes = new byte[chars.Length * 2];

    for (int i = 0; i < chars.Length; i++)
        Array.Copy(BitConverter.GetBytes(chars[i]), 0, bytes, i * 2, 2);

    return bytes;
}

public static string ByteArray2String(byte[] bytes)
{
    char[] chars = new char[bytes.Length / 2];

    for (int i = 0; i < chars.Length; i++)
        chars[i] = BitConverter.ToChar(bytes, i * 2);

    return new string(chars);
}

除了选择的答案,如果你使用。net 3.5或。net 3.5 CE,你必须指定解码的第一个字节的索引,以及解码的字节数:

string result = System.Text.Encoding.UTF8.GetString(byteArray, 0, byteArray.Length);
string result = ASCIIEncoding.UTF8.GetString(byteArray);

我在这篇文章中看到了一些答案,这可能被认为是完整的基础知识,因为我在c#编程中有几种方法来解决相同的问题。唯一需要考虑的是纯UTF-8和带有BOM的UTF-8之间的区别。

Last week, at my job, I needed to develop one functionality that outputs CSV files with a BOM and other CSV files with pure UTF-8 (without a BOM). Each CSV file encoding type will be consumed by different non-standardized APIs. One API reads UTF-8 with a BOM and the other API reads without a BOM. I needed to research the references about this concept, reading the "What's the difference between UTF-8 and UTF-8 without BOM?" Stack Overflow question, and the Wikipedia article "Byte order mark" to build my approach.

最后,我的c#编程的UTF-8编码类型(BOM和纯)需要类似于下面的例子:

// For UTF-8 with BOM, equals shared by Zanoni (at top)
string result = System.Text.Encoding.UTF8.GetString(byteArray);

//for Pure UTF-8 (without B.O.M.)
string result = (new UTF8Encoding(false)).GetString(byteArray);

试试这个控制台应用程序:

static void Main(string[] args)
{
    //Encoding _UTF8 = Encoding.UTF8;
    string[] _mainString = { "Hello, World!" };
    Console.WriteLine("Main String: " + _mainString);

    // Convert a string to UTF-8 bytes.
    byte[] _utf8Bytes = Encoding.UTF8.GetBytes(_mainString[0]);

    // Convert UTF-8 bytes to a string.
    string _stringuUnicode = Encoding.UTF8.GetString(_utf8Bytes);
    Console.WriteLine("String Unicode: " + _stringuUnicode);
}