How to convert decimal to binary with large numbers?

Asked

Viewed 1,909 times

1

I am creating a program in C# language to perform conversions I am currently implementing the binary -> decimal and decimal -> binary.

How can I do the conversion if user type large numbers as I should treat this?

Buttons

private void Btn_Decimal_Binario_Click(object sender, EventArgs e)
        {
            try
            {
                //Armazena o valor em decimal, convertendo o texto em tipo inteiro
                int ValorDecimal = int.Parse(txt_decimal.Text);

                //Realiza a chamada do metodo e armazena resultado na textbox
                txtBinario.Text = DecimalParaBinario(ValorDecimal);
            }
            catch (Exception)
            {
                MessageBox.Show("Verifique o preenchimento das informações no formulario ! ", "Alerta !", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void Btn_Binario_Decimal_Click(object sender, EventArgs e)
        {
            try
            {
                //Armazena o valor em binario,convertendo o texto em tipo inteiro
                int ValorBinario = int.Parse(txt_Binario_2.Text);

                //Realiza a chamada do metodo e armazena resultado na textbox
                txt_decimal_2.Text = BinarioParaDecimal(ValorBinario);
            }
            catch (Exception)
            {
                MessageBox.Show("Verifique o preenchimento das informações no formulario ! ", "Alerta !", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        } 

Conversions

static string DecimalParaBinario(int n)  
        {
            int resto;
            string result = string.Empty;
            while (n > 0)
            {
                resto = n % 2;
                n /= 2;
                result = resto.ToString() + result;
            }

            return result.ToString();
        }

        static string BinarioParaDecimal(int n)  
        {
            //Conversão Binario em Decimal
            int bin, dec = 0, resto, basenum = 1;
            bin = n;
            while (n > 0)
            {
                resto = n % 10;
                dec = dec + resto * basenum;
                basenum = basenum * 2;
                n = n / 10;
            }
            return dec.ToString();
        }
  • 1

    What is "big numbers"?

  • How should you treat ? declare a variable of large number.. because you declare int for Valordecimal???? is not a Value value Decimal

  • Hello @Matheusgonçalves welcome to Stackoverflow. Could you give us an example of these "big numbers"?

  • For example even putting in Decimal if I type for example 1231231231231231231231231232312312312 it throws error

  • @Matheusgonçalves Take a look at [tour]. You can accept an answer if it solved your problem. You can vote on every post on the site as well. Did any help you more? Need something to be improved?

1 answer

2

Browser other questions tagged

You are not signed in. Login or sign up in order to post.