I am a beginner learning c#. I have coded a method that turns a two digit integer in a sequence of 16 bits
// takes input from user and convert it
private void Button_Click(object sender, RoutedEventArgs e)
{
string input = key.Text;
string mykey = "";
foreach (var item in input)
{
mykey += Binary(item);
}
key.Text = mykey;
}
private string Binary(Char ch)
{
string result = string.Empty;
int asciiCode;
char[] bits = new char[8];
asciiCode = (int)ch;
result = Convert.ToString(asciiCode, 2);;
bits = result.PadLeft(8, '0').ToCharArray();
return string.Join("",bits);
}
It might be a bit complicated but it is working. However my main problem is that I want to invert the process: ie from a sequence such as 0011000100110010 I should retrieve the int which is 12. Can someone help me to get on the right track?
Any help is greatly appriciated