65.9K
CodeProject is changing. Read more.
Home

Iranian system encoding standard

starIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIconemptyStarIcon

1.92/5 (4 votes)

Oct 24, 2011

CPOL
viewsIcon

31834

There doesn't seem to be any encoding for the 'Iranian system encoding standard' in .NET

Someone posted a question about how to convert from the Iranian system encoding standard[^] into Unicode using .NET. I just had a look and this is a really interesting question. There are a few encodings in .NET that might be derived from the 'Iran System encoding standard', but they don't appear to be as described in the Wikipedia page[^].
708: Arabic (ASMO 708) - different
720: Arabic (DOS) - different
864: Arabic (864) - different
1256: Arabic (Windows) - different 
10004: Arabic (Mac) - not found
20420: IBM EBCDIC (Arabic) - not found
28596: Arabic (ISO) - different
I thought it would be fun to try to write it then:
class IranianSystemEncoding
{
    static char[] ByteToChar;
    static Byte[][] CharToByte;

    static IranianSystemEncoding()
    {
        InitializeData();
    }

    static void InitializeData()
    {
        var iranSystem = new int[] { 0x06F0, 0x06F1, 0x06F2, 0x06F3, 0x06F4, 0x06F5, 0x06F6, 0x06F7, 0x06F8, 0x06F9, 0x060C, 0x0640, 0x061F, 0xFE81, 0xFE8B, 0x0621, 0xFE8D, 0xFE8E, 0xFE8F, 0xFE91, 0xFB56, 0xFB58, 0xFE95, 0xFE97, 0xFE99, 0xFE9B, 0xFE9D, 0xFE9F, 0xFB7C, 0xFB7C, 0xFEA1, 0xFEA3, 0xFEA5, 0xFEA7, 0x062F, 0x0630, 0x0631, 0x0632, 0x0698, 0xFEB1, 0xFEB3, 0xFEB5, 0xFEB7, 0xFEB9, 0xFEBB, 0xFEBD, 0xFEBF, 0x0637, 0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x2561, 0x2562, 0x2556, 0x2555, 0x2563, 0x2551, 0x2557, 0x255D, 0x255C, 0x255B, 0x2510, 0x2514, 0x2534, 0x252C, 0x251C, 0x2500, 0x253C, 0x255E, 0x255F, 0x255A, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256C, 0x2567, 0x2568, 0x2564, 0x2565, 0x2559, 0x2558, 0x2552, 0x2553, 0x256B, 0x256A, 0x2518, 0x250C, 0x2588, 0x2584, 0x258C, 0x2590, 0x2580, 0x0638, 0xFEC9, 0xFECA, 0xFECC, 0xFECB, 0xFECD, 0xFECE, 0xFED0, 0xFECF, 0xFED1, 0xFED3, 0xFED5, 0xFED7, 0xFB8E, 0xFB90, 0xFB92, 0xFB94, 0xFEDD, 0xFEFB, 0xFEDF, 0xFEE1, 0xFEE3, 0xFEE5, 0xFEE7, 0x0648, 0xFEE9, 0xFEEC, 0xFEEB, 0xFBFD, 0xFBFC, 0xFBFE, 0x00A0 };
        ByteToChar = new char[256];
        // ascii first
        for (int i = 0; i < 128; i++) ByteToChar[i] = (char)i;
        // non-ascii
        for (int i = 128; i < 256; i++) ByteToChar[i] = (char)iranSystem[i - 128];

        // ok now reverse
        CharToByte = new Byte[256][];
        for (int i = 0; i < 256; i++)
        {
            char ch = (char)ByteToChar[i];
            var low = ch & 0xff;
            var high = ch >> 8 & 0xff;

            var lowCharToByte = CharToByte[high];
            if (lowCharToByte == null)
            {
                lowCharToByte = new Byte[256];
                CharToByte[high] = lowCharToByte;
            }
            lowCharToByte[low] = (byte)(i);
        }
    }

    public static String GetString(byte[] bytes)
    {
        var sb = new System.Text.StringBuilder();
        foreach (var b in bytes)
        {
            sb.Append(ByteToChar[b]);
        }
        return sb.ToString();
    }

    public static Byte[] GetBytes(string str)
    {
        var mem = new System.IO.MemoryStream();
        foreach (var ch in str)
        {
            var high = ch >> 8 & 0xff;
            var lowCharToByte = CharToByte[high];
            Byte res = 0;
            if (lowCharToByte != null)
            {
                var low = ch & 0xff;
                res = lowCharToByte[low];
            }
            if (res == 0) res = 0xff;
            mem.WriteByte(res);
        }
        return mem.ToArray();
    }
}
I have no clue if it works or not. My Visual Studio gets confused with left and right when I try to put some of those characters in the source. This was good fun though.