0

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

mikey
  • 1,339
  • 5
  • 22
  • 43
  • 1
    and this: http://stackoverflow.com/questions/736533/how-do-you-convert-a-string-to-ascii-to-binary-in-c – LINQ2Vodka Nov 02 '13 at 06:54
  • thank you, but maybe my question is not very clear. I want to do the opposite, I have my binary string, I want to get back an int so, form 0011000100110010 I want to get 12. – mikey Nov 02 '13 at 07:02
  • is it what you're looking for? int output = Convert.ToInt32(input, 2); – LINQ2Vodka Nov 02 '13 at 07:06
  • I tried but it seems that is not working `Additional non-parsable characters are at the end of the string.` is the error. I think is related to the fact that I am padding my string in order to get 16 bits... – mikey Nov 02 '13 at 07:10
  • this returns 12594: int s2 = Convert.ToInt32("0011000100110010", 2). This will return 12: int s2 = Convert.ToInt32("1100", 2). What i'm doing wrong? :) – LINQ2Vodka Nov 02 '13 at 07:14
  • Use Trim, that will remove extra while spaces including new line. – Akash Kava Nov 02 '13 at 07:52
  • 1
    In what world is "0011000100110010" the binary represantation of 12? – Boluc Papuccuoglu Nov 02 '13 at 08:42
  • well it is a bit tricky... it is the result from my method, prhaps I need to fix it. I was just trying to figure out a way to convert a int into a 16 long string of 1 and 0 – mikey Nov 02 '13 at 10:25

2 Answers2

1

Given the fact that you are learning C#, I will give you a simple, straightforward example even if it is not optimal or fancy. I think it would serve you purpose better.

  static int GetInt(string value)
    {
        double result = 0d;//double
        IEnumerable<char> target = value.Reverse();
        int index = 0;
        foreach (int c in target)
        {
            if (c != '0')
                result += (c - '0') * Math.Pow(2, index);
            index++;
        }

        return (int)result;
    }

This code will work with any padding. Also, you can change it to Int16 if you will or extend it as you want. Also, it assumes that the given string has the least significant bit at the end (little endian).

Bogdan Beda
  • 758
  • 5
  • 12
0
 var int16 = Convert.ToInt16("0011000100110010", 2);
Sameer
  • 3,124
  • 5
  • 30
  • 57