Click here to Skip to main content
13,836,489 members
Rate this:
Please Sign up or sign in to vote.
See more:
<pre> public static BitArray ConvertHexToBitArray(string hexData)
            if (hexData == null)
                return null;
            BitArray ba = new BitArray(4* hexData.Length);
            for (int i = 0; i < hexData.Length; i++)
                byte b = byte.Parse(hexData[i].ToString(), NumberStyles.HexNumber);
                for (int j = 0; j < 4; j++)
                    ba.Set(i * 4 + j, (b & (1 << (3 - j))) != 0);
            return ba;

<pre>private void button5_Click(object sender, EventArgs e)
        { //label1.Text = HexTobyteArray.BitArrayToString(array);
            var array = HexTobyteArray.ConvertHexToBitArray(textBox1.Text);
         //  string a = HexTobyteArray.BitArrayToString(array);
            foreach (bool b in array)
                Button[] btns = new Button[4];
                btns[0] = this.button1;
                btns[1] = this.button2;
                btns[2] = this.button3;
                btns[3] = this.button4;

                // label1.Text = a;                
                for (int i = 0; i < 4; i++)
                    for (int j = 0; j < 4; j++)
                        if (b == true)
                            btns[i].BackColor = Color.Green;
                            btns[i].BackColor = Color.Red;



What I have tried:

my problem is arrays to change back Color buttons,
pleass help me
Updated 18-May-18 14:40pm
OriginalGriff 18-May-18 7:28am
This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with.
Use the "Improve question" widget to edit your question and provide better information.
BillWoodruff 18-May-18 19:43pm
I would characterize this question as reflecting a strategy which is probably 'over-kill' ... but not that unclear. I remind myself that there are still people who "think" in hex ... even though I don't know any :)
Gerry Schmitz 18-May-18 12:38pm
What's the point of all this? There's probably a less obscure way to maintain a set of "button properties".
BillWoodruff 18-May-18 19:04pm
Why are you using string input in hex format here ?

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Note: the technique shown here will not work with hex string prefixed with '0x, or '0X: if you want to allow that, test for, and strip out, those prefixes. Or, switch the conversion technique to:[^] ... if you do that, you will not have the advantage of 'TryParse's inherent validation.

0. make a list of your Buttons at the Form level ... for re-use:
private List<Button> buttons;

// in the Form 'Load Event
buttons = new List<Button> { button1, button2, button3, button4 };
1. validate you have a string in 'legal' hex format, then use bit-shifting to test:
int hextoint;

if (Int32.TryParse(textBox1.Text, NumberStyles.HexNumber, CultureInfo.CurrentCulture, out hextoint))
    for (int i = 0; i < buttons.Count; i++)
        buttons[i].BackColor = ((hextoint & 1) == 1)
        ? Color.Green
        : Color.Red;

        hextoint = hextoint >> 1;
    throw new ArgumentException("bad hex input");
Member 13812740 20-May-18 9:43am
Thank's bill
This work :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Cookies | Terms of Service
Web01 | 2.8.190114.1 | Last Updated 20 May 2018
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100