jueves, 5 de abril de 2012

Convertir Numero entre Decimal y Binario

Objectivo
Convertir numeros de Decimales a Binarios y de Binario a Decimal
 
Codigo C#
 
        private string ConvertDecimalToBinary(string decimaltoconvertto)
        {
            #region Determinar el binario
            int pruebaByte;

            StringBuilder sb = new StringBuilder();

            if (int.TryParse(decimaltoconvertto, out pruebaByte))
            {
                do
                {
                    if (pruebaByte % 2 == 0)
                    {
                        sb.Append("0");

                        pruebaByte = pruebaByte / 2;
                    }

                    else
                    {
                        sb.Append("1");

                        pruebaByte = pruebaByte / 2;
                    }

                } while (pruebaByte > 0);
            }

            else
            {
                return "Valor no valido! Favor introducir un valor entre 0-255";
            }
            #endregion Determinar el binario

            #region Manipular el binario
            // Convertir el valor en una matriz
            char[] binary = sb.ToString().ToArray();

            // invierto el orden de los elementos
            char[] reversed = binary.Reverse().ToArray();

            /*********************************************
            * Agregar codigo para manipular los bits     *
            *********************************************/

            // construir el binario ya ordenado
            StringBuilder sb2 = new StringBuilder();

            foreach (char item in reversed)
            {
                sb2.Append(item);
            }
            #endregion Manipular el binario

            #region Para representar el binario con 8 bits
            int by;

            int.TryParse(sb2.ToString(), out by);

            return by.ToString("00000000");
            #endregion Para representar el binario con 8 bits
        }

        private string ConvertBinaryToDecimal(string binarytoconvertto)
        {
            int mydecimal = 0;

            int f;

            for (int i = 0; i < binarytoconvertto.Length; i++)
            {
                if (new string(binarytoconvertto.ToCharArray().Reverse().ToArray()[i], 1) != "0")
                {
                    int.TryParse(new string(binarytoconvertto.ToCharArray().Reverse().ToArray()[i], 1), out f);

                    mydecimal += ((int)f) * ((int)Math.Pow(2, i));
                }

                else
                {
                    continue;
                }
            }

            return mydecimal.ToString();
        }
Codigo VB
Private Function ConvertDecimalToBinary(decimaltoconvertto As String) As String
 '#Region "Determinar el binario"
 Dim pruebaByte As Integer

 Dim sb As New StringBuilder()

 If Integer.TryParse(decimaltoconvertto, pruebaByte) Then
  Do
   If pruebaByte Mod 2 = 0 Then
    sb.Append("0")

    pruebaByte = pruebaByte / 2
   Else

    sb.Append("1")

    pruebaByte = pruebaByte / 2

   End If
  Loop While pruebaByte > 0
 Else

  Return "Valor no valido! Favor introducir un valor entre 0-255"
 End If
 '#End Region

 '#Region "Manipular el binario"
 ' Convertir el valor en una matriz
 Dim binary As Char() = sb.ToString().ToArray()

 ' invierto el orden de los elementos
 Dim reversed As Char() = binary.Reverse().ToArray()

 '********************************************
'            * Agregar codigo para manipular los bits     *
'            ********************************************


 ' construir el binario ya ordenado
 Dim sb2 As New StringBuilder()

 For Each item As Char In reversed
  sb2.Append(item)
 Next
 '#End Region

 '#Region "Para representar el binario con 8 bits"
 Dim by As Integer

 Integer.TryParse(sb2.ToString(), by)

 Return by.ToString("00000000")
 '#End Region
End Function

Private Function ConvertBinaryToDecimal(binarytoconvertto As String) As String
 Dim mydecimal As Integer = 0

 Dim f As Integer

 Dim i As Integer = 0
 While i < binarytoconvertto.Length
  If New String(binarytoconvertto.ToCharArray().Reverse().ToArray()(i), 1) <> "0" Then
   Integer.TryParse(New String(binarytoconvertto.ToCharArray().Reverse().ToArray()(i), 1), f)

   mydecimal += DirectCast(f, Integer) * DirectCast(Math.Pow(2, i), Integer)
  Else

   Continue While
  End If
  System.Math.Max(System.Threading.Interlocked.Increment(i),i - 1)
 End While

 Return mydecimal.ToString()
End Function
Descripcion: