日期:2014-05-17  浏览次数:20931 次

关于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(
     &