首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

关于ASCII码 怎么转成字符串有关问题

2013-03-19 
关于ASCII码 如何转成字符串问题。本帖最后由 l7890590p 于 2013-03-15 13:58:13 编辑我利用一个串口通信工

关于ASCII码 如何转成字符串问题。
本帖最后由 l7890590p 于 2013-03-15 13:58:13 编辑
 我利用一个串口通信工具。 对面发来msg的ASCIICODE = 5 

                byte[] array = System.Text.Encoding.ASCII.GetBytes(msg);
                int asciicode = (int)(array[0]);
                ----asciicode的值 此时为5 -----

 在网上查询ascii码表,ASCII码为5时对应的字符为ENQ 。
 为什么我通过代码转换成字符串之后显示的是 “|” 类似这么一个竖杠的字符。
 我想通过把ASCII通过转换字符串变成 "ENQ" 改如何做?谢谢 ascii
[解决办法]

public static void Main() 
  {
      // Create an ASCII encoding.
      Encoding ascii = Encoding.ASCII;

      // A Unicode string with two characters outside the ASCII code range.
      String unicodeString =
          "This unicode string contains two characters " +
          "with codes outside the ASCII code range, " +
          "Pi (\u03a0) and Sigma (\u03a3).";
      Console.WriteLine("Original string:");
      Console.WriteLine(unicodeString);

      // Save the positions of the special characters for later reference.
      int indexOfPi = unicodeString.IndexOf('\u03a0');
      int indexOfSigma = unicodeString.IndexOf('\u03a3');

      // Encode the string.
      Byte[] encodedBytes = ascii.GetBytes(unicodeString);
      Console.WriteLine();
      Console.WriteLine("Encoded bytes:");
      foreach (Byte b in encodedBytes) 
      {
          Console.Write("[{0}]", b);
      }
      Console.WriteLine();

      // Notice that the special characters have been replaced with
      // the value 63, which is the ASCII character code for '?'.
      Console.WriteLine();
      Console.WriteLine(
          "Value at position of Pi character: {0}",
          encodedBytes[indexOfPi]
          );
      Console.WriteLine(
          "Value at position of Sigma character: {0}",


          encodedBytes[indexOfSigma]
          );

      // Decode bytes back to a string.
      // Notice missing the Pi and Sigma characters.
      String decodedString = ascii.GetString(encodedBytes);
      Console.WriteLine();
      Console.WriteLine("Decoded bytes:");
      Console.WriteLine(decodedString);
  }

热点排行