but i can't figure out how to calculate the 2 16bit-binary value: An example shown below.
public partial class Form1 : Form
{
uint byhex1 = 0;
uint byhex2 = 0;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void BTN_operation_Click(object sender, EventArgs e)
{
if (RB_andgate.Checked)
{
BTN_operation.Text = "&";
byhex1 = Convert.ToUInt16(TXB_hexvalue1.Text, 16);
LBL_binary1.Text = Convert.ToString(byhex1, 2).PadLeft(16, '0');
byhex2 = Convert.ToUInt16(TXB_hexvalue2.Text, 16);
LBL_binary2.Text = Convert.ToString(byhex2, 2).PadLeft(16, '0');
LBL_totalhex.Text = "0 X " + (byhex1 & byhex2).ToString("X4");
}
if (RB_orgate.Checked)
{
BTN_operation.Text = "|";
byhex1 = Convert.ToUInt32(TXB_hexvalue1.Text, 16);
LBL_binary1.Text = Convert.ToString(byhex1, 2).PadLeft(16, '0');
byhex2 = Convert.ToUInt32(TXB_hexvalue2.Text, 16);
LBL_binary2.Text = Convert.ToString(byhex2, 2).PadLeft(16, '0');
LBL_totalhex.Text = "0 X " + (byhex1 | byhex2).ToString("X4");
}
if (RB_xorgate.Checked)
{
BTN_operation.Text = "^";
byhex1 = Convert.ToUInt32(TXB_hexvalue1.Text, 16);
LBL_binary1.Text = Convert.ToString(byhex1, 2).PadLeft(16, '0');
byhex2 = Convert.ToUInt32(TXB_hexvalue2.Text, 16);
LBL_binary2.Text = Convert.ToString(byhex2, 2).PadLeft(16, '0');
LBL_totalhex.Text = "0 X " + (byhex1 ^ byhex2).ToString("X4");
}
}
}
how to put an exception just like in picture below in my code?

VulpesRank #55 · 29.2K reputationPosted Oct 21, 2011, 1:17 AM EDT
Hemant KumarRank #1,202 · 1.5K reputationPosted Oct 20, 2011, 11:44 PM EDT
There is no need to code tons of codes, loops, to convert hex to binary string. Convert.ToInt32 function is very useful for this purpose.
Let's convert the "A" character to binary format.
private string hex2binary(string hexvalue) { string binaryval = ""; binaryval = Convert.ToString(Convert.ToInt32(hexvalue, 16), 2); return binaryval; }hex2binary("1a"); will return "11010";
hex2bianry("1a2c"); will return "1101000101100"; and so on.
Keep in mind that this hex to binary conversion style uses 32 bit integer number.