【发布时间】:2015-03-11 17:20:36
【问题描述】:
我的程序正在使用 LibUSB 与 USB 设备通信,该设备具有按钮和 LED,用于向设备发送信息并从设备读取信息。通信是在字节数组中完成的,最长可达 1024 字节,但几乎只有前 8 个字节很重要。读取时,我们读取字节数组,我想显示哪个按钮被按下或哪个 LED 亮了。因此,我认为字典是使用的最佳解决方案 - 按钮或 LED 名称的字符串值和键的字节数组。或者可能是相反的;字符串是键,数组是值。
public class DeviceInput
{
public static byte[] PowerArray = { 1, 0, 0, 16, 0, 0, 0, 0 };
public string Name { get; set; }
public byte[] InputArray { get; set; }
public byte[] LEDArray { get; set; }
public DeviceInput(string name, byte[] input, byte[] led)
{
Name = name;
InputArray = input;
LEDArray = led;
}
public static Dictionary<byte[], string> InputDictionary()
{
var dict = new Dictionary<byte[], string>();
dict.Add(PowerArray, "Power Button");
return dict;
}
}
在我的主程序中:
public Dictionary<byte[], string> inputDict = DeviceInput.InputDictionary();
在我的读取方法中,我将读入的字节数组并将前 8 个字节存储到本地数组中,我使用.ContainsKey() 来查看字典是否包含键(字节数组),然后会显示给用户的值(字符串)。
byte[] data =
{
readBuffer[0], readBuffer[1], readBuffer[2], readBuffer[3],
readBuffer[4], readBuffer[5], readBuffer[6], readBuffer[7]
};
if (inputDict.ContainsKey(data))
{
Console.WriteLine("You pressed: " + inputDict[data]);
}
设备中的 readBuffer 工作正常,数组填充完美,与我创建的字节数组 (PowerArray) 完全相同,所以我不确定 ContainsKey 是如何工作的。即使我将字典切换到<string, byte[]> 并尝试使用ContainsValue 而不是ContainsKey,但没有成功。
字典是获取这些数据的最佳方式吗?我应该以不同的方式加载数据吗?我是否错误地访问它?感谢您的指导。
【问题讨论】:
-
数组使用默认引用相等,因此即使它们包含相等的值,它们也不会评估为相等。试试
array1.Equals(array2);。当你创建字典时,你可以传入一个IEqualityComparer<TKey>,告诉它如何比较键来处理这个问题。
标签: c# dictionary data-structures key-value libusb