关于c# 2进制数据高位和低位的有关问题
关于c# 2进制数据高位和低位的问题
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace ConsoleApplication19
{
class Program
{
static void Main(string[] args)
{
string temp = "58.29.FE.C0.A8.12.64.57";
byte[] bytes = Program.Hex2ByteArr(temp);
MyData data = new MyData(bytes);
}
/// <summary>
/// Hex2ByteArr
/// </summary>
/// <param name="newString"></param>
/// <returns></returns>
public static byte[] Hex2ByteArr(string newString)
{
int len = newString.Replace(".", "").Length / 2;
byte[] arr = new byte[len];
for (int i = 0; i < len; i++)
{
arr[i] = Convert.ToByte(newString.Replace(".", "").Substring(i * 2, 2), 16);
}
return arr;
}
}
class MyData
{
BitArray bits;
public MyData(byte[] bytes)
{
if (bytes == null || bytes.Length != 8) throw new ArgumentException("must be an array of 8 bytes");
//Array.Reverse(bytes);
this.bits = new BitArray(bytes);
}
public int ObjectId
{
get { return GetValue(0, 6); } // 6bit 对象ID
}
public float ObjectLength
{
get { return GetValue(6, 8) * 0.2f; } // 8bit; 单位0.2 对象长度
}
public float VelocityY
{
get { return (GetValue(14, 11) - 1024) * 0.1f; } // 11bit,偏移1024; 单位0.1米/秒 速度Y坐标
}
public float VelocityX
{
get { return (GetValue(25, 11) - 1024) * 0.1f; } // 11bit,偏移1024; 单位0.1米/秒 速度X坐标
}
public float RangeY
{
get { return (GetValue(36, 14) - 8192) * 0.032f; } // 14bit,偏移8192; 单位0.032米 范围Y坐标
}
public float RangeX
{
get { return (GetValue(50, 14) - 8192) * 0.032f; } // 14bit,偏移8192; 单位0.032米 范围X坐标
}
private int GetValue(int startBit, int length)
{
int value = 0;