解决方案 »

  1.   

     static void Run()
            {
    //"45 00 80 00 10 00 00 02 01 01 00 0D 66 86 00 00 00 00 00 00 FF FF 14 0E 03 1C 12 15 20 00 14 0E 03 1C 12 15 2E 00 1A AA A0 00 00 00 00 00 00 00 00 00 00 00 00 00 05 02 01 00 08 02 01 00 10 04 90 00 00 00 FF "
               
                string data =  File.ReadAllText("F:\\1.txt", Encoding.Default).Replace(" ", "");
                int result = 0;
                string output = string.Empty;
                byte[] array = new byte[1024];
                //string[] redata = data.Split(' ');
                //byte[] newb = new byte[redata.Length];
                //for (int i = 0; i < redata.Length; i++)
                //{
                //    if (i != 0)
                //    {
                //        Console.Write(" ");
                //    }            //    //将十进制转换成16进制Begin
                //    newb[i] =Convert.ToByte(redata[i], 16);
                //    //将十进制转换成16进制End            //    Console.Write(newb[i].ToString());
                //}
                for (int i = 0; i < data.Length; i=i+2)
                {
                    string di = data.Substring(i, 2);
                    for (int j = 0; j < 2; j++)
                    {
                        string dj = di.Substring(j, 1);
                        #region
                        switch (dj)
                        {
                            case "a":
                            case "A":
                                {
                                    result = result * 16 + 10; break;
                                }
                            case "B":
                            case "b":
                                {
                                    result = result * 16 + 11; break;
                                }
                            case "C":
                            case "c":
                                {
                                    result = result * 16 + 12; break;
                                }
                            case "D":
                            case "d":
                                {
                                    result = result * 16 + 13; break;
                                }
                            case "E":
                            case "e":
                                {
                                    result = result * 16 + 14; break;
                                }
                            case "F":
                            case "f":
                                {
                                    result = result * 16 + 15; break;
                                }
                            default: {
                                result = result * 16 + (string.IsNullOrEmpty(dj.Trim()) ? 0 : Convert.ToInt32(dj));
                                break;
                            }
                        }
                        #endregion       
                    }
                    output += result.ToString()+"|";
                    array[i] = Convert.ToByte(result);
                    result = 0;
                }
                Console.WriteLine(output);
                Console.WriteLine(Encoding.Unicode.GetString(array));
                File.WriteAllBytes("F://2.txt", array);        }
    我的代码