ASCIIEncoding In Windows Phone 7
Asked Answered
A

6

19

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?

Albino answered 26/10, 2010 at 9:23 Comment(2)
How does character encoding standards related to encyrption? As I understand it SHA1 (or any other hashing algorithm) takes a stream of bytes and generates a hash (also a short sequence of bytes). Where does character encoding enter the equation?Coronary
Currently Generating a Hash in C# (For WP7) (Unicode) generates a different hash to which PHP Generates (ASCII). I saw another question on here saying that using 2 different encodings generate 2 different hash's.Albino
P
44

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;
    }
Perambulate answered 26/10, 2010 at 10:43 Comment(0)
C
4

Not really seeing any detail in your question this could be off track. You are right Silverlight has no support for the ASCII encoding.

However I suspect that in fact UTF8 will do what you need. Its worth bearing in mind that a sequence of single byte ASCII only characters and the same set of characters encoded as UTF-8 are identical. That is the the complete ASCII character set is repeated verbatim by the first 128 single byte code points in UTF-8.

Coronary answered 26/10, 2010 at 10:17 Comment(0)
O
4

I have a Silverlight app that writes CSV files, which have to be encoded in ASCII (using UTF-8 causes accented characters to show up wrong when you open the files in Excel).

Since Silverlight doesn't have an Encoding.ASCII class, I implemented one as follows. It works for me, hope it's useful to you as well:

/// <summary>
/// Silverlight doesn't have an ASCII encoder, so here is one:
/// </summary>
public class AsciiEncoding : System.Text.Encoding
{
    public override int GetMaxByteCount(int charCount)
    {
        return charCount;
    }
    public override int GetMaxCharCount(int byteCount)
    {
        return byteCount;
    }
    public override int GetByteCount(char[] chars, int index, int count)
    {
        return count;
    }
    public override byte[] GetBytes(char[] chars)
    {
        return base.GetBytes(chars);
    }
    public override int GetCharCount(byte[] bytes)
    {
        return bytes.Length;
    }
    public override int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex)
    {
        for (int i = 0; i < charCount; i++)
        {
            bytes[byteIndex + i] = (byte)chars[charIndex + i];
        }
        return charCount;
    }
    public override int GetCharCount(byte[] bytes, int index, int count)
    {
        return count;
    }
    public override int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex)
    {
        for (int i = 0; i < byteCount; i++)
        {
            chars[charIndex + i] = (char)bytes[byteIndex + i];
        }
        return byteCount;
    }
}
Orcein answered 16/8, 2012 at 1:57 Comment(0)
J
1

I had similar problem using Xamarin (Mono) for Android where I'm using Portable Class Library and they don't support Econding.ASCII.

Instead, the only working solution (except doing it manually) is this one

Uri.EscapeDataString(yourString);

See this answer which provide additional information.

Jermainejerman answered 21/8, 2013 at 17:58 Comment(0)
I
1

I started from @Hans Passant 's answer and I rewrote it with Linq :

/// <summary>
/// Gets an encoding for the ASCII (7-bit) character set.
/// </summary>
/// <see cref="https://mcmap.net/q/629893/-asciiencoding-in-windows-phone-7"/>
/// <param name="s">A character set.</param>
/// <returns>An encoding for the ASCII (7-bit) character set.</returns>
public static byte[] StringToAscii(string s)
{
    return (from char c in s select (byte)((c <= 0x7f) ? c : '?')).ToArray();
}

You may want to remove the call to ToArray() and return a IEnumerable<byte> instead of byte[].

Illusory answered 12/9, 2015 at 20:51 Comment(1)
More elegant, but probably much less efficient than Hans's approach. Sometimes it's hard to beat the performance of a raw for loop.Drus
C
0

According to this MS forum thread, Windows Phone 7 does not support Encoding.ASCII.

Cobbler answered 26/10, 2010 at 9:28 Comment(1)
Well, Is there other ways around this (E.G. Custom Class or Library)? Or does the Framework itself have to support ASCII?Albino

© 2022 - 2024 — McMap. All rights reserved.