例如,10进制数88转成16进制为0058,再ASCII编码得“\x30\x30\x35\x38”。如何用C#实现

解决方案 »

  1.   

    10-16转换:
    int I, J; string S;
                I = 123;
                S = I.ToString("X");
                J = int.Parse(S,System.Globalization.NumberStyles.AllowHexSpecifier);16-ascii转换:string[] keys = Console.ReadLine().Split(' ');//读取用空格分成的16进制码
                foreach (string key in keys)
                {
                    int code = Convert.ToInt32(key, 16);//将16进制字符串转换成其ASCII码(实际是Unicode码)
                    char c = (char)code;//取得这个Unicode码表示的char(强制转换就行)
                    Console.Write(c.ToString());//输出。
                }
                Console.ReadLine()
      

  2.   

    // 10进制数88转成16进制为0058,再ASCII编码得“\x30\x30\x35\x38”
    ulong source = 88L;
    var result = source.ToString("X4").Select(c =>string.Format(@"\x{0:X2}",(byte)c)).ToList();
    result.ForEach(c=>Console.Write(c));结果:
    \x30\x30\x35\x38
      

  3.   


                    int i= 88;
                    string s = i.ToString("X4");
                    string r = "";
                    string pre=@"\x";
                    foreach(char item in s)
                    {
                        r += pre+((int)item).ToString("X");
                    }