根据文件填充一组复选框

发布于 2024-11-13 23:04:41 字数 420 浏览 4 评论 0原文

我试图弄清楚如何使用 BinaryReader 读取文件(不是由我的程序创建的),并相应地选中或取消选中一组复选框。

我设法弄清楚复选框是这样存储的:

Checkbox 1 = 00 01
Checkbox 2 = 00 02
Checkbox 3 = 00 04
Checkbox 4 = 00 08
Checkbox 5 = 00 10
Checkbox 6 = 00 20
Checkbox 7 = 00 40
Checkbox 8 = 00 60
Checkbox 9 = 00 80
Checkbox 10 = 01 00
Checkbox 11 = 02 00
etc

因此,如果在文件中检查了复选框 1、2、6 和 10,则十六进制值将是: 01 23。我如何将其分解,以便程序中的正确复选框将被选中?

I'm trying to figure out how to read a file (not created by my program), using a BinaryReader, and checking or unchecking a set of checkboxes accordingly.

I've managed to figure out that the checkboxes are stored as such:

Checkbox 1 = 00 01
Checkbox 2 = 00 02
Checkbox 3 = 00 04
Checkbox 4 = 00 08
Checkbox 5 = 00 10
Checkbox 6 = 00 20
Checkbox 7 = 00 40
Checkbox 8 = 00 60
Checkbox 9 = 00 80
Checkbox 10 = 01 00
Checkbox 11 = 02 00
etc

So if, in the file, checkboxes 1, 2, 6, and 10 where checked the hex value would be: 01 23. How would I break this down so that the correct checkboxes in program would be checked?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

妄想挽回 2024-11-20 23:04:41

我认为你的例子中有一个错字。复选框 8 不应该是 0060,而应该是 0080。因此 123 意味着位:1、2、6、9(而不是 10)。

像这样:

Checkbox 01 = 00 01
Checkbox 02 = 00 02
Checkbox 03 = 00 04
Checkbox 04 = 00 08
Checkbox 05 = 00 10
Checkbox 06 = 00 20
Checkbox 07 = 00 40
Checkbox 08 = 00 80
Checkbox 09 = 01 00
Checkbox 10 = 02 00

要检查设置了哪些复选框,您可以使用如下代码:

// var intMask = Convert.ToInt32("0123", 16); // use this line if your input is string
var intMask = 0x0123";
var bitArray = new BitArray(new[] { intMask });
for (var i = 0; i < 16; i++)
{
    var isCheckBoxSet = bitArray.Get(i);
    if (isCheckBoxSet)
        Console.WriteLine("Checkbox {0} is set", i + 1);
}

输出:

Checkbox 1 is set
Checkbox 2 is set
Checkbox 6 is set
Checkbox 9 is set

因此,带有复选框的代码将像这样简单:

var checkboxes = new List<CheckBox>();
var intMask = 0x0123;
var bitArray = new BitArray(new[] { intMask });
for (var i = 0; i < 16; i++)
    checkboxes.Add(new CheckBox { Checked = bitArray.Get(i) });

I think there is a typo in your example. Checkbox 8 should not be 0060, but rather 0080. So 123 would mean bits: 1, 2, 6, 9 (not 10).

Like this:

Checkbox 01 = 00 01
Checkbox 02 = 00 02
Checkbox 03 = 00 04
Checkbox 04 = 00 08
Checkbox 05 = 00 10
Checkbox 06 = 00 20
Checkbox 07 = 00 40
Checkbox 08 = 00 80
Checkbox 09 = 01 00
Checkbox 10 = 02 00

To check what check box is set you could use code like this:

// var intMask = Convert.ToInt32("0123", 16); // use this line if your input is string
var intMask = 0x0123";
var bitArray = new BitArray(new[] { intMask });
for (var i = 0; i < 16; i++)
{
    var isCheckBoxSet = bitArray.Get(i);
    if (isCheckBoxSet)
        Console.WriteLine("Checkbox {0} is set", i + 1);
}

The output:

Checkbox 1 is set
Checkbox 2 is set
Checkbox 6 is set
Checkbox 9 is set

So your code with checkboxes would be as simple as this:

var checkboxes = new List<CheckBox>();
var intMask = 0x0123;
var bitArray = new BitArray(new[] { intMask });
for (var i = 0; i < 16; i++)
    checkboxes.Add(new CheckBox { Checked = bitArray.Get(i) });
狼亦尘 2024-11-20 23:04:41

按正确的顺序保留 CheckBox[]List 以及 CheckBox 引用,以便您可以通过索引引用它们。您将循环遍历各个位值并使用计数器来跟踪与该位关联的索引:

short setBits = 0x0123; # short because it is 2 bytes.
short currentBit = 0x0001;
// loop through the indexes (assuming 16 CheckBoxes or fewer)
for (int index = 0; index < checkBoxes.Length; index++) {
    checkBoxes[index].Checked = (setBits & currentBit) == currentBit;
    currentBit <<= 1; // shift one bit left;
}

Keep a CheckBox[] or List<CheckBox> with the CheckBox references in the correct order so that you can refer to them by index. You would loop through the individual bit values and use a counter to keep track of the index associated with that bit:

short setBits = 0x0123; # short because it is 2 bytes.
short currentBit = 0x0001;
// loop through the indexes (assuming 16 CheckBoxes or fewer)
for (int index = 0; index < checkBoxes.Length; index++) {
    checkBoxes[index].Checked = (setBits & currentBit) == currentBit;
    currentBit <<= 1; // shift one bit left;
}
我不是你的备胎 2024-11-20 23:04:41

这就足够了 - 适当调整上限。

for(int i = 0; i < 15; ++i) {
    Checkbox[i + 1].Checked = (yourbits && (1 << i)) != 0
}

This'd be adequate - adjust the upper limit appropriately.

for(int i = 0; i < 15; ++i) {
    Checkbox[i + 1].Checked = (yourbits && (1 << i)) != 0
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文