.Net 8-bit Encoding

后端 未结 5 2103
春和景丽
春和景丽 2020-12-19 13:14

I\'m working on serial port, transmitting and receiving data to some hardware at 8bit data. I would like to store it as string to facilitate comparison, and preset data are

相关标签:
5条回答
  • 2020-12-19 13:29

    I think you should use a byte array instead. For comparison you can use some method like this:

    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;
    }
    
    0 讨论(0)
  • 2020-12-19 13:32

    Why not just use an array of bytes instead? It would have none of the encoding problems you're likely to suffer with the text approach.

    0 讨论(0)
  • 2020-12-19 13:35

    You could use base64 encoding to convert from byte to string and back. No problems with code pages or weird characters that way, and it'll be more space-efficient than hex.

    byte[] toEncode; 
    string encoded = System.Convert.ToBase64String(toEncode);
    
    0 讨论(0)
  • 2020-12-19 13:44

    Use the Hebrew codepage for Windows-1255. Its 8 bit.
    Encoding enc = Encoding.GetEncoding("windows-1255");

    I missunderstod you when you wrote "1-255", thought you where refereing to characters in codepage 1255.

    0 讨论(0)
  • 2020-12-19 13:54

    Latin-1 aka ISO-8859-1 aka codepage 28591 is a useful codepage for this scenario, as it maps values in the range 128-255 unchanged. The following are interchangeable:

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

    The following code illustrates the fact that for Latin1, unlike Encoding.Default, all characters in the range 0-255 are mapped unchanged:

    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;
    }
    
    0 讨论(0)
提交回复
热议问题