0

我有一个长代码,例如 02 10 18 14 cb f5 50 d3 14 00 0d 00 0900 05 00 04 7b 28 00 00 00 00 28 cb。

我要解码 00 09

到它的相等值,即 2.25 我写了这个程序,但是当我有另一个包含00 1E而不是 00 09 的代码时,问题就出现了。

00 09 必须转换为 0000 0000 0000 1001 然后最后两位应该是 0.25 的倍数,其余的必须是 1 的倍数才能达到 2.25 如何更改我的程序以识别像 00 1E 这样的十六进制值?

        double q=0.0,w=0.0,x,y;
        char []chrxB1=new char [4];
        char[] chrxB2 = new char[4];
        char[] chrxB3 = new char[4];
        char[] chrxB4 = new char[4];
        char []sn=new char [74];
        string ss, xB1, xB2, xB3, xB4, yB1, yB2, yB3, yB4;

        ss = textBox1.Text;
        xB1 = ss[37].ToString();
        xB2 = ss[36].ToString();
        xB3 = ss[34].ToString();
        xB4 = ss[33].ToString();

        yB1 = ss[43].ToString();
        yB2 = ss[42].ToString();
        yB3 = ss[40].ToString();
        yB4 = ss[39].ToString();

        string b1 = Convert.ToString(int.Parse(xB1), 2);
        chrxB1= b1.ToArray();
        if (chrxB1.Length!=0)
            for (int i = 0; i < chrxB1.Length; i++)
            {
                if (i == 0)
                   q=(int.Parse(chrxB1[i].ToString()) * (1));
                else if (i == 1)
                    q = q + (int.Parse(chrxB1[i].ToString()) * (2));
                else if (i == 2)
                    w = int.Parse(chrxB1[i].ToString()) * (1);
                else
                    w = w + (int.Parse(chrxB1[i].ToString()) * (2));
            }
        q=q*0.25;
        string b2 = Convert.ToString(int.Parse(xB2), 2);
        chrxB2 = b2.ToArray();
        if (chrxB2.Length != 0)
            for (int i = 0; i < chrxB2.Length; i++)
            {
                if (i == 0)
                    w = w + (int.Parse(chrxB2[i].ToString()) * (4));
                else if (i == 1)
                    w = w + (int.Parse(chrxB2[i].ToString()) * (8));
                else if (i == 2)
                    w = int.Parse(chrxB2[i].ToString()) * (16);
                else
                    w = w + (int.Parse(chrxB2[i].ToString()) * (32));
            }
        string b3 = Convert.ToString(int.Parse(xB3), 2);
        chrxB3 = b3.ToArray();
        if (chrxB3.Length != 0)
            for (int i = 0; i < chrxB3.Length; i++)
            {
                if (i == 0)
                    w = w + (int.Parse(chrxB3[i].ToString()) * (64));
                else if (i == 1)
                    w = w + (int.Parse(chrxB3[i].ToString()) * (128));
                else if (i == 2)
                    w = int.Parse(chrxB3[i].ToString()) * (256);
                else
                    w = w + (int.Parse(chrxB3[i].ToString()) * (512));
            }
        string b4 = Convert.ToString(int.Parse(xB4), 2);
        chrxB4 = b4.ToArray();
        if (chrxB4.Length != 0)
            for (int i = 0; i < chrxB4.Length; i++)
            {
                if (i == 0)
                    w=w+(int.Parse(chrxB4[i].ToString()) * (1024));
                else if (i == 1)
                    w = w + (int.Parse(chrxB4[i].ToString()) * (2048));
                else if (i == 2)
                    w = int.Parse(chrxB4[i].ToString()) * (4096);
                else
                    w = w + (int.Parse(chrxB4[i].ToString()) * (8192));
            }
        x = w + q;
        textBox2.Text = x.ToString();
    }
}
4

2 回答 2

2

如果你问

“问:如何在 C# 中解析十六进制字符串?”

, 答案是:

int.Parse(hexValue, System.Globalization.NumberStyles.HexNumber);

于 2013-08-29T04:18:28.823 回答
0

我要对你说实话——我不是 100% 确定你在问什么。但是,我相信您在问如何转换表示以 16 为基数的整数的字符串。

var num = Int32.Parse("001e", NumberStyles.HexNumber); 

NumberStyles在 中System.Globalization,所以一定要包括它或完全限定它。

于 2013-08-29T04:20:28.827 回答