我有一種感覺,這與BitConverter.ToUInt16
wiorks的方式,但我不能爲我的生活弄清楚爲什麼這是給我時髦的數據。爲什麼這不會取消兩個最重要的位?
我需要忽略進入端口的前兩位,並將其餘位轉換爲16位無符號整數。
我已經嘗試顛倒數組,顛倒我的面具,做了兩個,不同的轉換和各種奇怪的事情。
進來的兩個字節是第一個是最重要的。第一個字節的前兩位需要取消設置。
有人能指出我正確的方向嗎?
byte[] buffer = new byte[2];
int count = port.Read(buffer, 0, buffer.Length);
Console.WriteLine("0: {0}", BitConverter.ToString(buffer));
ushort value = BitConverter.ToUInt16(buffer, 0);
Console.WriteLine("1: {0}", value);
value = (ushort)(value & 0x3FFF);
Console.WriteLine("2: {0}", value);
下面是使用BitConverter.ToUInt16然後AND運算與0x3FFF
掩模當某些樣本數據。
0: 80-00
1: 128
2: 128 <-- this should be 0
0: 80-00
1: 128
2: 128 <-- should be 0
0: 01-00
1: 1
2: 1 <-- should be 1, as it is
0: 80-00
1: 128
2: 128 <-- should be 0
0: 80-00
1: 128
2: 128 <-- should be 0
逆轉陣列使我的數據是這樣的:
0: 00-01
1: 256
2: 256 <-- should be 1
0: 01-80
1: 32769
2: 1 <- not sure what this should be, probably 1 though
你最終在'value'中做了什麼? – DWright
@DWright我將它添加到問題中,謝謝。 –