Conversão de Binário para Decimal fracionário e vice-versa
-
quinta-feira, 4 de março de 2010 13:55Hey, estou com uma dúvida em um programa de conversão de binários para decimais e vice-versa não-exatos.
Consegui converter com números inteiros, mas ao colocar números fracionários não deu certo. Já pesquisei e não achei nada relacionado a isso. Alguém tem alguma solução ou pelo menos uma função que ajude? Por favor (:
Todas as Respostas
-
quinta-feira, 4 de março de 2010 16:59Moderador
Prezado,
Esse carinha já fez o que você está precisando:
http://www.geekpedia.com/tutorial137_Converting-from-decimal-to-binary-and-back.html
André Alves de Lima
Visite o meu site: http://andrealveslima.spaces.live.com
Me siga no Twitter: @andrealveslima- Marcado como Resposta Harley AraujoOwner segunda-feira, 8 de março de 2010 14:10
-
quinta-feira, 4 de março de 2010 17:32Moderador
Hey, estou com uma dúvida em um programa de conversão de binários para decimais e vice-versa não-exatos.
aqui tem um exemplo completo, como fazer isto:
Consegui converter com números inteiros, mas ao colocar números fracionários não deu certo. Já pesquisei e não achei nada relacionado a isso. Alguém tem alguma solução ou pelo menos uma função que ajude? Por favor (:
Just Be Humble Malange! -
quinta-feira, 4 de março de 2010 17:36ModeradorJust Be Humble Malange!
- Create a sample Windows application in Visual Studio. NET.
- In the form:
- Add three label controls named label1, label2, and label3.
- Add three text box controls named textBox1, textBox2, and textBox3.
- Place a button control named button1 and set its Text property to �To Base�.
- Place another button control named button2 and set its Text property to �To Base�.
- Write the following declarations as data members of the form class:
Collapseconst int base10 = 10;
char[] cHexa = new char[]{'A','B','C','D','E','F'};
int[] iHexaNumeric = new int[] {10,11,12,13,14,15};
int[] iHexaIndices = new int[] {0,1,2,3,4,5};
const int asciiDiff = 48;
- Include the functions as member methods in the form class module.
This function takes two arguments; the integer value to be converted and the base value (2, 8, or 16) to which the number is converted to:
Collapsestring DecimalToBase(int iDec, int numbase)
{
string strBin = "";
int[] result = new int[32];
int MaxBit = 32;
for(; iDec > 0; iDec/=numbase)
{
int rem = iDec % numbase;
result[--MaxBit] = rem;
}
for (int i=0;i<result.Length;i++)
if ((int) result.GetValue(i) >= base10)
strBin += cHexa[(int)result.GetValue(i)%base10];
else
strBin += result.GetValue(i);
strBin = strBin.TrimStart(new char[] {'0'});
return strBin;
}
This function takes two arguments; a string value representing the binary, octal, or hexadecimal value and the corresponding integer base value respective to the first argument. For instance, if you pass the first argument value "1101", then the second argument should take the value "2".
Collapseint BaseToDecimal(string sBase, int numbase)
{
int dec = 0;
int b;
int iProduct=1;
string sHexa = "";
if (numbase > base10)
for (int i=0;i<cHexa.Length;i++)
sHexa += cHexa.GetValue(i).ToString();
for(int i=sBase.Length-1; i>=0; i--,iProduct *= numbase)
{
string sValue = sBase[i].ToString();
if (sValue.IndexOfAny(cHexa) >=0)
b=iHexaNumeric[sHexa.IndexOf(sBase[i])];
else
b= (int) sBase[i] - asciiDiff;
dec += (b * iProduct);
}
return dec;
}
- Write the code in the click event of the buttons as follows:
On clicking the first button, you can convert a decimal number to any base you select in the ComboBox.
Collapseprivate void button1_Click(object sender, System.EventArgs e)
{
textBox3.Text = DecimalToBase(Int32.Parse(textBox1.Text),
Int32.Parse(comboBox1.Text));
}
On clicking the second button, you can convert the base number you have selected in the ComboBox (binary, octal, or hexadecimal) to a decimal number.
Collapseprivate void button2_Click(object sender, System.EventArgs e)
{
textBox3.Text = BaseToDecimal(textBox1.Text,
Int32.Parse(comboBox1.Text)).ToString();
}

