ASCIIEncoding In Windows Phone 7

Dean picture Dean · Oct 26, 2010 · Viewed 12.9k times · Source

Is there a way to use ASCIIEncoding in Windows Phone 7?

Unless I'm doing something wrong Encoding.ASCII doesn't exist and I'm needing it for C# -> PHP encryption (as PHP only uses ASCII in SHA1 encryption).

Any suggestions?

Answer

Hans Passant picture Hans Passant · Oct 26, 2010

It is easy to implement yourself, Unicode never messed with the ASCII codes:

    public static byte[] StringToAscii(string s) {
        byte[] retval = new byte[s.Length];
        for (int ix = 0; ix < s.Length; ++ix) {
            char ch = s[ix];
            if (ch <= 0x7f) retval[ix] = (byte)ch;
            else retval[ix] = (byte)'?';
        }
        return retval;
    }