Click here to Skip to main content
15,884,751 members
Articles / Programming Languages / C#
Tip/Trick

Iranian system encoding standard

Rate me:
Please Sign up or sign in to vote.
1.92/5 (4 votes)
24 Oct 2011CPOL 31.1K   3   7
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:
C#
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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
France France
I am a French programmer.
These days I spend most of my time with the .NET framework, JavaScript and html.

Comments and Discussions

 
GeneralMy vote of 1 Pin
Masterhame1-Jun-19 19:43
Masterhame1-Jun-19 19:43 
Question[My vote of 1] my vote of 1 because it doesn't work at all! Pin
Jalal Khordadi14-Jan-15 4:59
Jalal Khordadi14-Jan-15 4:59 
AnswerRe: [My vote of 1] my vote of 1 because it doesn't work at all! Pin
Pascal Ganaye20-Jan-15 9:01
Pascal Ganaye20-Jan-15 9:01 
Questionvar in vs 2005 Pin
hamid norouzi23-Jun-13 0:01
hamid norouzi23-Jun-13 0:01 
Questionthe type 'var' is unknown in vs 2005 Pin
hamid norouzi22-Aug-12 12:53
hamid norouzi22-Aug-12 12:53 
AnswerRe: the type 'var' is unknown in vs 2005 Pin
Pascal Ganaye22-Aug-12 23:11
Pascal Ganaye22-Aug-12 23:11 
GeneralRe: the type 'var' is unknown in vs 2005 Pin
Babak Bakhshayesh10-Apr-13 6:36
Babak Bakhshayesh10-Apr-13 6:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.