【问题标题】:ASCIIEncoding In Windows Phone 7Windows Phone 7 中的 ASCII 编码
【发布时间】:2010-10-26 09:23:32
【问题描述】:

有没有办法在 Windows Phone 7 中使用 ASCIIEncoding?

除非我做错了什么Encoding.ASCII 不存在并且我需要它来进行 C# -> PHP 加密(因为 PHP 仅在 SHA1 加密中使用 ASCII)。

有什么建议吗?

【问题讨论】:

  • 字符编码标准如何与加密相关?据我了解,SHA1(或任何其他散列算法)采用字节流并生成散列(也是一个短字节序列)。字符编码在哪里进入方程?
  • Currently Generate a Hash in C# (For WP7) (Unicode) 生成 PHP 生成的不同哈希值 (ASCII)。我在这里看到另一个问题,说使用 2 种不同的编码会生成 2 种不同的哈希。

标签: c# windows-phone-7 encoding ascii


【解决方案1】:

自己很容易实现,Unicode 从来不会和 ASCII 码混淆:

    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;
    }

【讨论】:

    【解决方案2】:

    在您的问题中没有真正看到任何细节,这可能会偏离轨道。没错,Silverlight 不支持 ASCII 编码。

    但我怀疑实际上 UTF8 会满足您的需求。值得记住的是,单字节纯 ASCII 字符序列和编码为 UTF-8 的同一组字符是相同的。也就是说,完整的 ASCII 字符集由 UTF-8 中的前 128 个单字节代码点逐字重复。

    【讨论】:

      【解决方案3】:

      我有一个写入 CSV 文件的 Silverlight 应用程序,这些文件必须以 ASCII 编码(使用 UTF-8 会导致在 Excel 中打开文件时重音字符显示错误)。

      由于 Silverlight 没有 Encoding.ASCII 类,我实现了一个如下。它对我有用,希望它对你也有用:

      /// <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;
          }
      }
      

      【讨论】:

        【解决方案4】:

        我在使用 Xamarin (Mono) for Android 时遇到了类似的问题,我使用的是便携式类库,但它们不支持 Econding.ASCII。

        相反,唯一可行的解​​决方案(手动操作除外)是这个

        Uri.EscapeDataString(yourString);
        

        请参阅this answer,其中提供了更多信息。

        【讨论】:

          【解决方案5】:

          我从@Hans Passant 的回答开始,然后用 Linq 重写了它:

          /// <summary>
          /// Gets an encoding for the ASCII (7-bit) character set.
          /// </summary>
          /// <see cref="http://stackoverflow.com/a/4022893/1248177"/>
          /// <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();
          }
          

          您可能希望删除对ToArray() 的调用并返回IEnumerable&lt;byte&gt; 而不是byte[]

          【讨论】:

          • 更优雅,但可能比 Hans 的方法效率低得多。有时很难超越原始 for 循环的性能。
          【解决方案6】:

          根据这个微软论坛thread,Windows Phone 7 不支持Encoding.ASCII

          【讨论】:

          • 好吧,还有其他方法可以解决这个问题(例如自定义类或库)吗?还是框架本身必须支持 ASCII?
          猜你喜欢
          • 2011-06-10
          • 2012-01-17
          • 1970-01-01
          • 2015-06-19
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-02-04
          相关资源
          最近更新 更多