.Net 8-bit encoding

I am working on a serial port, sending and receiving data to some hardware with 8-bit data. I would like to save it as a string for easy comparison, and the predefined data is stored as a string or hexadecimal format in an XML file. I found out that only when using Encoding.Default encoding ANSI, the 8-bit data is converted correctly and is easily reversible. ASCII coding will work only for 7-bit data, and UTF8 or UTF7 does not work either, since I use some character from 1-255. Encoding.Default will be just fine, but I read on MSDN that it depends on the setting of the OS code page, which means that it can behave differently when setting up another code page. I make extensive use of GetBytes () and GetString using encoding, but would like the safe and portable method to work all the time in any configuration. Any idea or best suggestion for this?

+4
source share
5 answers

Latin-1 aka ISO-8859-1 aka codepage 28591 is a useful code page for this scenario, as it displays 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 displayed 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; } 
+13
source

Why not just use an array of bytes? He would not have any encoding problems that you would probably suffer with using the text approach.

+9
source

I think you should use a byte array. For comparison, you can use some method:

 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; } 
+2
source

Use the Jewish code page for Windows-1255. Its 8 bits.
Encoding enc = Encoding.GetEncoding ("windows-1255");

I will miss you when you wrote "1-255", I thought where you refer to the characters in code page 1255.

+1
source

You can use base64 encoding to convert from byte to string and vice versa. No problem with code pages or weird characters, and it will be more economical than hex.

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

Source: https://habr.com/ru/post/1276991/


All Articles