6

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

Imane Fateh
  • 2,418
  • 3
  • 19
  • 23
Mohsen
  • 323
  • 1
  • 6
  • 15
  • 3
    It's not really possible since ASCII doesn't support Persian. What do you want to do with the ASCII string? – acfrancis Nov 13 '13 at 10:32
  • 1
    I need this method to use in AutoCAD. – Mohsen Nov 13 '13 at 10:42
  • 1
    In VBA, when I get a Persian text, in code Editor I see ? marks. – Mohsen Nov 13 '13 at 11:00
  • Well, you can't use ASCII. I don't know this for a fact but AutoCAD probably supports Unicode. If it doesn't you could try another encoding that supports Persian. – acfrancis Nov 13 '13 at 11:03
  • possible duplicate of [ASCIIEncoding.ASCII.GetBytes() Returning Unexpected Value](http://stackoverflow.com/questions/15963421/asciiencoding-ascii-getbytes-returning-unexpected-value) – CodeCaster Nov 13 '13 at 11:14

2 Answers2

5

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);
VahidN
  • 18,457
  • 8
  • 73
  • 117
1

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.

Afshar Mohebi
  • 10,479
  • 17
  • 82
  • 126