我是个初学者.请各位大侠帮忙.字符串如何转化为16进制,16进制转化为字符串
如下面的一个命令
STRING STR="AT"怎么转化还有一个问题命令后面加一个回车键要怎么写?
先谢谢

解决方案 »

  1.   

    回车键是   System.Environment.NewLine
      

  2.   

    正好有现成的代码,给你贴过去
    /// <summary>
    /// 将指定的字符串转化为16进制的字符串
    /// </summary>
    /// <param name="text"></param>
    /// <returns></returns>
    private string StrToHexStr(string text)
    {
    string result = "";
    byte[] bytes = Encoding.GetEncoding("GB2312").GetBytes(text);
    foreach(byte byt in bytes)
    {
    result += Convert.ToString((short)byt, 16);
    }
    return result;
    } /// <summary>
    /// 将指定的十六进制字符串转化为普通字符串
    /// </summary>
    /// <param name="text"></param>
    /// <returns></returns>
    private string HexStrToStr(string text)
    {
    string result = "";
    byte[] bytes = new byte[text.Length / 2];
    int i = 0, r = 0;       
    while (i < text.Length)
    {
    bytes[r] = byte.Parse(text.Substring(i, 2), System.Globalization.NumberStyles.HexNumber);
    i += 2;
    r++;
    }
    result = Encoding.GetEncoding("GB2312").GetString(bytes);
    return result;
    }