开发者

ANSI Encoding implementation for Silverlight

开发者 https://www.devze.com 2023-03-03 03:17 出处:网络
I need Encoding implementation of some codepage in my Silverlight app. Particular开发者_StackOverflow中文版ly I need to read non-Engligh file names from zip-archive (being opened Application.GetResour

I need Encoding implementation of some codepage in my Silverlight app. Particular开发者_StackOverflow中文版ly I need to read non-Engligh file names from zip-archive (being opened Application.GetResourceStream).

Silverlight contains only Unicode encodings (Encoding.UTF8, Encoding.Unicode, Encoding.BigEndianUnicode). Encoding.GetEncoding throws exception for other encoding names.

But I need Encoding class implementation for some ANSI codepage (866 particularly). On desktop I'd get it via Encoding.GetEncoding(866).

Where can I get the simplest implementation?

p.s. I understand that the question hardly relates Silverlight, but without mentioning it I'll be suggested to use Encoding.GetEncoding I guess..


From http://en.wikipedia.org/wiki/Code_page_866 you can build a map and convert your stuff to UTF-8.


Thanks to @Bala_R, i get 866 codepage and write:

    private byte[] translateInto866(string fileName)
    {
        const byte startCode1 = 128;        // А, 0410
        const byte startCode2 = 224;        // р, 0440

        var result = new byte[fileName.Length];
        int i = 0;
        foreach (char c in fileName)
        {
            if (c >= 'А' && c <= 'п')
            {
                result[i] = (byte)(((byte)(c - 'А')) + startCode1);
            }
            else if (c > 'п' && c <= 'я')
            {
                result[i] = (byte)(((byte)(c - 'р')) + startCode2);
            }
            else
            {
                result[i] = (byte) c;
            }
            i++;
        }
        return result;
    }

Now we only need to understand what codepage was used to encode file name. We have its unicode representation in manifest and a representation in some encoding inside zip. It should be not very hard to find appropriate encoding. But in my case I just know that if it's not utf8 then it's 866.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号