需要根据复选框的位组合解决方案 [英] Need solution for combinations of bits according to checkboxes

查看:54
本文介绍了需要根据复选框的位组合解决方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我有8个复选框,我想在One Byte值中设置这些复选框的值。也就是说,每个复选框值(0或1)占用一位。如何在C#中为此编写逻辑?



示例Checkbox1设置为1,其余复选框设置为0,则1字节的值为0x01(二进制0000 0001) )。同样明智的我需要代码中所有复选框的组合。



请有人帮助我。

Hi,

I have 8 checkboxes and I want to set the value of these checkboxes in One Byte value. That is each checkbox values(either 0 or 1) occupy one bit. How to write logic for this in C# ?

Example Checkbox1 is set to 1 and remaining checkboxes set to 0 then the value of 1 byte is 0x01(in binary 0000 0001). Like wise i need combinations for all the checkboxes in code.

Please anyone help me.

推荐答案

试试这样..





try like this..


byte b = 0x01;
BitArray flags = new BitArray(new byte[] { b });
CheckBox1.Checked = flags[0];
CheckBox2.Checked = flags[1];
CheckBox3.Checked = flags[2];
CheckBox4.Checked = flags[3];
CheckBox5.Checked = flags[4];
CheckBox6.Checked = flags[5];
CheckBox7.Checked = flags[6];
CheckBox8.Checked = flags[7];





将8位数据(BitArray)转换为字节:



Convert 8 bit data (BitArray) to byte:

byte[] bytes = new byte[1];
 flags.CopyTo(bytes, 0);
 byte result = bytes[0];


为了防止你提出这样的基本问题,你最好通过学习二进制和十六进制数学来实现。



我对此非常认真。谷歌二进制数学和按位运算符并开始阅读。你不能在不知道这些东西的情况下编写代码。
To prevent you from asking basic questions like this, you would be best served by learning to do math in binary and hexadecimal.

I'm dead serious on this. Google "Binary math" and "bitwise operators" and start reading. You can't go through life writing code without knowing this stuff.


我将假设除了根据字节值设置CheckBox的Checked属性之外。 。作为Karthik整齐地告诉你该怎么做,你也想读取CheckBoxes的检查状态,并构造一个字节值。
I'm going to assume that in addition to setting the 'Checked property of the CheckBoxes based on a byte value ... as Karthik neatly shows you how to do, that you also want to read the CheckBoxes' checked state, and construct a byte value.
// declare a list of CheckBoxes
private List<checkbox> cbList = new List<checkbox>(); 

// somewhere in code, perhaps in a Form Load Event initialize the List
cbList.AddRange(new CheckBox[]
{
  checkBox1, checkBox2, checkBox3, checkBox4, checkBox5, checkBox6, checkBox7, checkBox8
});

// function to read the CheckBoxes and return a byte value
private byte getByteFromCheckBoxes()
{
    byte b = 0x0;

    for (int i = 0; i < 8; i++)
    {
        if(cbList[i].Checked) b += Convert.ToByte(Math.Pow(2, i));
    }

    return b;
}

// test the function at run-time
private void button1_Click(object sender, EventArgs e)
{
    byte checkByte = getByteFromCheckBoxes();
}

为了消除上面函数中的计算...如果从CheckBoxes生成一个字节经常发生我想要做的...我会考虑使用一个字典:

For eliminating the computation in the function above ... which I'd want to do if generating a byte from the CheckBoxes was occurring frequently ... I'd consider using a Dictionary:

private Dictionary<CheckBox, byte> dctChkByte;

// initialize somewhere in code
dctChkByte = new Dictionary<checkbox,>
{
    {checkBox1, 0x01},
    {checkBox2, 0x02},
    {checkBox3, 0x04},
    {checkBox4, 0x08},
    {checkBox5, 0x10},
    {checkBox6, 0x20},
    {checkBox7, 0x40},
    {checkBox8, 0x80},
};

// use in a function
private byte getByteFromCheckBoxes2()
{
    byte b2 = 0x0;

    foreach (CheckBox theCheckBox in dctChkByte.Keys)
    {
        if (theCheckBox.Checked) b2 += dctChkByte[theCheckBox];

        // note that you cannot do this: write Microsoft
        // and complain if this makes you unhappy !
        // b2 += (theCheckBox.Checked) ? dctChkByte[theCheckBox] : 0x0;
    }

    return b2;
}


这篇关于需要根据复选框的位组合解决方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆