代码之家  ›  专栏  ›  技术社区  ›  faulty

.NET 8位编码

  •  5
  • faulty  · 技术社区  · 16 年前

    我正在处理串行端口,向8bit数据的一些硬件发送和接收数据。为了便于比较,我希望将其存储为字符串,并且在XML文件中预设数据存储为字符串或十六进制格式。我发现,只有在使用encoding.default(即ansi编码)时,8bit数据才能正确转换,并且很容易可逆。ASCII编码只对7bit数据有效,而utf8或utf7也不能很好地工作,因为我使用的是1-255之间的一些字符。默认值会很好,但我在msdn上看到它依赖于OS代码页设置,这意味着它在配置的不同代码页上的行为可能会有所不同。我广泛使用getbytes()和getstring进行编码,但希望使用一种故障保护和可移植的方法,在任何配置下都能一直工作。对此有什么想法或更好的建议吗?

    5 回复  |  直到 16 年前
        1
  •  15
  •   to StackOverflow    16 年前

    拉丁语1 aka iso-8859-1 aka codepage 28591对于这种情况是一个有用的代码页,因为它映射128-255范围内的值不变。以下是可互换的:

    Encoding.GetEncoding(28591)
    Encoding.GetEncoding("Latin1")
    Encoding.GetEncoding("iso-8859-1")
    

    下面的代码说明了与编码不同的是,对于Latin1,0-255范围内的所有字符都是未更改的映射:

    static void Main(string[] args)
    {
    
        Console.WriteLine("Test Default Encoding returned {0}", TestEncoding(Encoding.Default));
        Console.WriteLine("Test Latin1 Encoding returned {0}", TestEncoding(Encoding.GetEncoding("Latin1")));
        Console.ReadLine();
        return;
    }
    
    private static bool CompareBytes(char[] chars, byte[] bytes)
    {
        bool result = true;
        if (chars.Length != bytes.Length)
        {
            Console.WriteLine("Length mismatch {0} bytes and {1} chars" + bytes.Length, chars.Length);
            return false;
        }
        for (int i = 0; i < chars.Length; i++)
        {
            int charValue = (int)chars[i];
            if (charValue != (int)bytes[i])
            {
                Console.WriteLine("Byte at index {0} value {1:X4} does not match char {2:X4}", i, (int) bytes[i], charValue);
                result = false;
            }
        }
        return result;
    }
    private static bool TestEncoding(Encoding encoding)
    {
        byte[] inputBytes = new byte[256];
        for (int i = 0; i < 256; i++)
        {
            inputBytes[i] = (byte) i;
        }
    
        char[] outputChars = encoding.GetChars(inputBytes);
        Console.WriteLine("Comparing input bytes and output chars");
        if (!CompareBytes(outputChars, inputBytes)) return false;
    
        byte[] outputBytes = encoding.GetBytes(outputChars);
        Console.WriteLine("Comparing output bytes and output chars");
        if (!CompareBytes(outputChars, outputBytes)) return false;
    
        return true;
    }
    
        2
  •  9
  •   Lasse V. Karlsen    16 年前

    为什么不使用字节数组呢?它不会有任何编码问题,您可能会遇到的文本方法。

        3
  •  2
  •   KovBal    16 年前

    我认为您应该改用字节数组。为了进行比较,您可以使用如下方法:

    static bool CompareRange(byte[] a, byte[] b, int index, int count)
    {
        bool res = true;
        for(int i = index; i < index + count; i++)
        {
            res &= a[i] == b[i];
        }
        return res;
    }
    
        4
  •  1
  •   user19264    16 年前

    使用Windows-1255的希伯来语代码页。它的8位。
    encoding enc=encoding.getencoding(“windows-1255”);

    当你写“1-255”的时候,我误解了你,以为你指的是代码页1255中的字符。

        5
  •  -2
  •   Matt Howells    16 年前

    您可以使用base64编码从字节转换为字符串并返回。这样的代码页和奇怪的字符没有问题,而且比十六进制更节省空间。

    byte[] toEncode; 
    string encoded = System.Convert.ToBase64String(toEncode);