Convert Extended ASCII into UTF8

905 views Asked by At

I was asked to solve an encoding problem in a file. It was expected to be in UTF8 but it was actually in extended ASCII.

The result is a file with cases like this:

Brasília; Eletrônicos e Informática Câmeras e Acessórios música

When it should actually be :

Brasília Eletrônicos e Informática Câmeras e Acessórios música

I solved it with this code :

private static string FixEncodingIssues(string str)
        {
            string fixedStr = str;

            foreach (KeyValuePair<string, string> pair in encodingErrosDic)
                fixedStr = fixedStr.Replace(pair.Key,pair.Value);
            
            return fixedStr;
        }

        private static Dictionary<string, string> encodingErrosDic = new Dictionary<string, string>()
        {
            { "Ã" , "Ã" },
            { "Ã\x81"  , "Á" },
            { "À" , "À" },
            { "Â" , "Â" },
            { "Ä" , "Ä" },
            { "Ã…" , "Å" },
            { "Ç" , "Ç" },
            { "È" , "È" },
            { "É" , "É" },
            { "Ê" , "Ê" },
            { "Ë" , "Ë" },
            { "ÃŒ" , "Ì" },
            { "Ã\x8D"  , "Í" },
            { "ÃŽ" , "Î" },
            { "Ã\x8F"  , "Ï" },
            { "Ã\x90"  , "Ð" },
            { "Ñ" , "Ñ" },
            { "Ã’" , "Ò"},
            { "Ó" , "Ó" },
            { "Ô" , "Ô" },
            { "Õ" , "Õ" },
            { "Ö" , "Ö" },
            { "×" , "×" },
            { "Ø" , "Ø" },
            { "Ù" , "Ù" },
            { "Ú" , "Ú" },
            { "Û" , "Û" },
            { "Ãœ" , "Ü" },
            { "Ã\x9D" , "Ý" },
            { "Ã\xA0" , "à" },
            { "á" , "á" },
            { "â" , "â" },
            { "ã" , "ã" },
            { "ä" , "ä" },
            { "Ã¥" , "å" },
            { "æ" , "æ" },
            { "ç" , "ç" },
            { "è" , "è" },
            { "é" , "é" },
            { "ê" , "ê"},
            { "ë" , "ë" },
            { "ì" , "ì" },
            { "î" , "î" },
            { "ï" , "ï" },
            { "Ã\xAD" , "í" },
            { "ð" , "ð" },
            { "ñ" , "ñ" },
            { "ò" , "ò" },
            { "ó" , "ó" },
            { "ô" , "ô" },
            { "õ" , "õ" },
            { "ö" , "ö" },
            { "ø" , "ø" },
            { "ù" , "ù" },
            { "ú" , "ú" },
            { "û" , "û" },
            { "ü" , "ü" },
            { "ý" , "ý" }
        };

I would like to know if there is a nicer way to solve this issue. I feel that my solution is too rough, it won't work for bytes not listed in the dictionary. I wished to know if there is a cleaner solution that doesn't involve listing all the extended cases and replacing them with equivalent UTF8 values.

0

There are 0 answers