/**
* 64 00 64 00
77 BE 9F 1A 2F DD 5E 40
equipnum = 6553700
equipnum = 6553700
equipnum = 6553700
equipnum = 6553700
*
* */
private void button4_Click(object sender, EventArgs e)
{
//int i = 123;
int i = 6553700;
byte[] intBuff = BitConverter.GetBytes(i); // 将 int 转换成字节数组
foreach (byte b in intBuff)
{
richTextBox1.AppendText(string.Format("{0} ", b.ToString("X2")));
}
i = BitConverter.ToInt32(intBuff, 0); // 从字节数组转换成 int
richTextBox1.AppendText("
");
double x = 123.456;
byte[] doubleBuff = BitConverter.GetBytes(x); // 将 double 转换成字节数组
foreach (byte b in doubleBuff)
{
richTextBox1.AppendText(string.Format("{0} ", b.ToString("X2")));
}
x = BitConverter.ToDouble(doubleBuff, 0); // 从字节数组转换成 double
// ushort数组 转 long(int64)
// 1、移位
ushort[] arr = new ushort[] { 100, 100, 0, 0 };
Int64 equipNum = 0;
for (int m = 0; m < 4; m++)
{
equipNum = equipNum | (arr[m] << m * 16);
}
richTextBox1.AppendText(string.Format("equipnum = {0}
", equipNum));
// 2、使用BitConverter,先转为字节,在转为long
byte[] by = new byte[8];
for (int m = 0; m < 4; m++)
{
byte[] b = BitConverter.GetBytes(arr[m]);
by[2*m] = b[0];
by[2*m+1] = b[1];
}
equipNum = BitConverter.ToInt64(by, 0);
richTextBox1.AppendText(string.Format("equipnum = {0}
", equipNum));
byte[] bys = new byte[8];
for (int m = 0; m < 4; m++)
{
byte[] b = BitConverter.GetBytes(arr[m]);
Array.Copy(b, 0, bys, 2 * m, 2);
}
equipNum = BitConverter.ToInt64(bys, 0);
richTextBox1.AppendText(string.Format("equipnum = {0}
", equipNum));
byte[] bya = new byte[8];
int k = 0;
foreach (var a in arr)
{
byte[] b = BitConverter.GetBytes(a);
Array.Copy(b, 0, bya, 2 * k, 2);
k += 1;
}
equipNum = BitConverter.ToInt64(bya, 0);
richTextBox1.AppendText(string.Format("equipnum = {0}
", equipNum));
}