Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert persian unicode to Ascii

I need to get the ASCII code of a Persian string to use it in a program. But the method below give the ? marks: "??? ????"

public string PerisanAscii()
    {

        //persian string 
        string unicodeString = "صبح بخیر";

        // Create two different encodings.
        Encoding ascii = Encoding.ASCII;
        Encoding unicode = Encoding.Unicode;

        // Convert the string into a byte array. 
        byte[] unicodeBytes = unicode.GetBytes(unicodeString);

        // Perform the conversion from one encoding to the other. 
        byte[] asciiBytes = Encoding.Convert(unicode, ascii, unicodeBytes);

        // Convert the new byte[] into a char[] and then into a string. 
        char[] asciiChars = new char[ascii.GetCharCount(asciiBytes, 0, asciiBytes.Length)];
        ascii.GetChars(asciiBytes, 0, asciiBytes.Length, asciiChars, 0);
        string asciiString = new string(asciiChars);

        return asciiString;
    }

Can you help me?

Best regards,
Mohsen

like image 484
Mohsen Avatar asked Nov 13 '13 10:11

Mohsen


2 Answers

You can convert Persian UTF8 data to Windows-1256 (Arabic Windows):

var enc1256 = Encoding.GetEncoding("windows-1256");
var data = enc1256.GetBytes(unicodeString);
System.IO.File.WriteAllBytes(path, data);
like image 113
VahidN Avatar answered Nov 05 '22 11:11

VahidN


ASCII does not support Persian. You may need old school Iran System encoding standard. This is determined by your Autocad application. I don't know if there is a direct Encoding in windows for it or not. But you can convert characters manually too. It's a simple mapping.

like image 1
Afshar Mohebi Avatar answered Nov 05 '22 11:11

Afshar Mohebi