Main Site Documentation

String to byte[] with encoding change


#1

Hello,
I need to send data to COM port, but in cp1251 or cp866 encoding.
How can i convert string in UTF8 encoding, to byte[] in cp866?
Or how to set encoding to SerialPort?

In Windows application I use:


  Encoding destinationEncoding = Encoding.GetEncoding(0x362);
  Encoding unicode = Encoding.Unicode;
  byte[] bytes = unicode.GetBytes("Test string"));
  byte[] buffer = Encoding.Convert(unicode, dstEncoding, bytes);
  SerialPort.Write(buffer, 0, buffer.Length);



#2

NETMF only has native support for UTF8 so any encoding other than that you’d have to do manually…[italic]yikes[/italic] :o


#3

This is a way, i found to convert encoding
Any optimisation ideas for faster converting?


        //symbols array
        private static readonly char[] CharMap866 = {
                                               'А', 'Б', 'В', 'Г', 'Д', 'Е', 'Ж', 'З', 'И', 'Й', 'К', 'Л', 'М', 'Н', 'О',
                                               'П', 'Р', 'С', 'Т', 'У', 'Ф', 'Х', 'Ц', 'Ч', 'Ш', 'Щ', 'Ъ', 'Ы', 'Ь', 'Э',
                                               'Ю', 'Я', 'а', 'б', 'в', 'г', 'д', 'е', 'ж', 'з', 'и', 'й', 'к', 'л', 'м',
                                               'н', 'о', 'п', 'р', 'с', 'т', 'у', 'ф', 'х', 'ц', 'ч', 'ш', 'щ', 'ъ', 'ы',
                                               'ь', 'э', 'ю', 'я', 'Ё', 'ё', 'Є', 'є', 'Ї', 'ї', 'Ў', 'ў', '№'
                                           };
        // symbols codes
        private static readonly byte [] CodeMap866 = {
                                              128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142,
                                              143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157,
                                              158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172,
                                              173, 174, 175, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235,
                                              236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 252
                                          };

        //get symbol code 
        private static byte GetCharCode(char charIn)
        {
            for(int i = 0; i < 73; i++)
            {
                if (charIn == CharMap866[i]) return CodeMap866[i];
            }
            return Encoding.UTF8.GetBytes(charIn.ToString())[0];
        }
        //convert input string to bytes
        public static byte[] GetBytes866(string strInput)
        {
            char[] inCharData = strInput.ToCharArray();
            byte[] outBytes = new byte[inCharData.Length];

            for (int i = 0; i < inCharData.Length; i++)
            {
                outBytes[i] =GetCharCode(inCharData[i]);
            }
            return outBytes;
        }

#4
return Encoding.UTF8.GetBytes(charIn.ToString())[0];

This is very bad :slight_smile:

try this

return (byte)charIn;

#5

Oh, yes ;D Thanks a lot!