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?
VulpesPosted Oct 21, 2011, 5:17 AM
Hemant KumarPosted Oct 21, 2011, 3:44 AM
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.
When we call hex2binary("A"); it will return "1010" similar samples below;Let's convert the "A" character to binary format.
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.