我有一個長碼,如02 10 18 14 cb f5 50 d3 14 00 0d 00 09
00 05 00 04 7b 28 00 00 00 00 28 cb。C#解碼(轉換器)
我要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();
}
}
你正在使用什麼樣的「編碼」?換句話說 - 你怎麼決定'00 09'應該等於'2.25'?你可以做一個簡單的例子,解釋你想要做什麼?這裏似乎有很多不必要的代碼(爲了得到你想要的答案)。讓所有人都感到難受! – Floris
什麼是2.25和0.25?我們在談論什麼樣的代碼?它是一個定點浮點數嗎? –