Foro de elhacker.net

Programación => .NET (C#, VB.NET, ASP) => Mensaje iniciado por: Meta en 23 Agosto 2014, 14:27 pm



Título: Convertir en binario a Hex y Decimal.
Publicado por: Meta en 23 Agosto 2014, 14:27 pm
Hola:

Cada botón valor1 al 3 cambia los bits que deseo del texbot Bin. Mi idea es conseguir leer esos bit para hacer el cambio.
(http://social.msdn.microsoft.com/Forums/getfile/514698)

Sólo debo cambiar un dígito o caracter, que ya funciona.

Código
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10.  
  11. namespace Sustituir_letra
  12. {
  13.    public partial class Form1 : Form
  14.    {
  15.        public Form1()
  16.        {
  17.            InitializeComponent();
  18.        }
  19.        const string Binario_Restaurar = "B00000, B00000, B00000, B00000, B00000, B00000, B00000, B00000";
  20.  
  21.        public static string Sustituir(string cadenaOrignal, int posicion, int longitud, string sustituto)
  22.        {
  23.            return cadenaOrignal.Substring(0, posicion)
  24.               + sustituto
  25.               + cadenaOrignal.Substring(posicion + longitud);
  26.        }
  27.  
  28.        private void button_Restaurar_Click(object sender, EventArgs e)
  29.        {
  30.            textBox_Bin.Text = Binario_Restaurar;
  31.        }
  32.  
  33.        private void button_Cambiar_Letra1_Click(object sender, EventArgs e)
  34.        {
  35.            textBox_Bin.Text = Sustituir(textBox_Bin.Text, 12, 1, "1");
  36.            textBox_Bin.Text = Sustituir(textBox_Bin.Text, 27, 1, "0");
  37.            textBox_Bin.Text = Sustituir(textBox_Bin.Text, 28, 1, "0");
  38.        }
  39.  
  40.        private void button_Cambiar_Letra2_Click(object sender, EventArgs e)
  41.        {
  42.            textBox_Bin.Text = Sustituir(textBox_Bin.Text, 2, 1, "1");
  43.            textBox_Bin.Text = Sustituir(textBox_Bin.Text, 3, 1, "1");
  44.            textBox_Bin.Text = Sustituir(textBox_Bin.Text, 4, 1, "1");
  45.            textBox_Bin.Text = Sustituir(textBox_Bin.Text, 5, 1, "1");
  46.        }
  47.  
  48.        private void button_Cambiar_Letra3_Click(object sender, EventArgs e)
  49.        {
  50.            textBox_Bin.Text = Sustituir(textBox_Bin.Text, 3, 1, "0");
  51.            textBox_Bin.Text = Sustituir(textBox_Bin.Text, 4, 1, "0");
  52.            textBox_Bin.Text = Sustituir(textBox_Bin.Text, 26, 1, "1");
  53.            textBox_Bin.Text = Sustituir(textBox_Bin.Text, 27, 1, "1");
  54.            textBox_Bin.Text = Sustituir(textBox_Bin.Text, 28, 1, "1");
  55.            textBox_Bin.Text = Sustituir(textBox_Bin.Text, 29, 1, "1");
  56.        }
  57.    }
  58. }
  59.  

Como se puede ver en el textbox en Bin en la imagen de arriba. Se muestra estos valores.

B00000, B00000, B00000, B00000, B00000, B00000, B00000


Quiero transformar estos valores binarios en Dec y Hex.

Dec sería: 0, 0, 0, 0, 0, 0, 0
En Hex sería: 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00

Desde el momento que se modifique el valor del Bin, automáticamente debe cambiar también el Dec y Hex. Tengo que lograr de alguna manera que lea los estados dentro del TextBox que están en binario que son de 5 bits cada uno, luego transformar esos binariso a Dec y Hex para mostrarlos en los otros textbox.

No me importa de alguna manera leer cada byte de 5 bits por decirlo de alguna manera que esté dentro del textbox y guardarlo en cada variable, luego hacemos la conversión de binario a Dec y a Hex.

¿Alguna idea?

Saludo.


Título: Re: Convertir en binario a Hex y Decimal.
Publicado por: Eleкtro en 23 Agosto 2014, 16:23 pm
Quiero transformar estos valores binarios en Dec y Hex.

Código
  1. ' bin
  2. Dim binStr As String = String.Format("B{0}", "00000")
  3.  
  4. ' bin a hex
  5. Dim hexStr As String = String.Format("0x{0}", Convert.ToString(Convert.ToInt32(binStr.Replace("B"c, String.Empty), 2I), 16I))
  6.  
  7. ' hex a int32
  8. Dim decVal As String = Convert.ToInt32(hexStr, 16I)

Code Converter | Provided by Telerik (http://converter.telerik.com/)


Título: Re: Convertir en binario a Hex y Decimal.
Publicado por: Meta en 23 Agosto 2014, 20:15 pm
Gracias.

Código
  1. // bin
  2.  
  3. {
  4. string binStr = string.Format("B{0}", "00000");
  5.  
  6. // bin a hex
  7. string hexStr = string.Format("0x{0}", Convert.ToString(Convert.ToInt32(binStr.Replace('B', string.Empty), 2), 16));
  8.  
  9. // hex a int32
  10. string decVal = Convert.ToInt32(hexStr, 16);
  11. }

Hay otro enlace también y es bueno saberlo.
http://www.developerfusion.com/tools/convert/vb-to-csharp/

Voy a hechar un ojo al código y luego les cuento si me ha funcionado.


Título: Re: Convertir en binario a Hex y Decimal.
Publicado por: Meta en 24 Agosto 2014, 19:46 pm
Gracias, me funciona, lo hice a mi manera.

Código
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10.  
  11. namespace Sustituir_letra
  12. {
  13.    public partial class Form1 : Form
  14.    {
  15.        public Form1()
  16.        {
  17.            InitializeComponent();
  18.        }
  19.        const string Binario_Restaurar = "B00000, B00000, B00000, B00000, B00000, B00000, B00000, B00000";
  20.  
  21.        public static string Sustituir(string cadenaOrignal, int posicion, int longitud, string sustituto)
  22.        {
  23.            return cadenaOrignal.Substring(0, posicion)
  24.               + sustituto
  25.               + cadenaOrignal.Substring(posicion + longitud);
  26.        }
  27.  
  28.        private void button_Restaurar_Click(object sender, EventArgs e)
  29.        {
  30.            textBox_Bin.Text = Binario_Restaurar;
  31.        }
  32.  
  33.        private void button_Cambiar_Letra1_Click(object sender, EventArgs e)
  34.        {
  35.            Convertir();
  36.        }
  37.  
  38.        public void Convertir()
  39.        {
  40.            string[] tokens = textBox_Bin.Text.Split(',');
  41.            var numbers = tokens.Select(x => Convert.ToUInt16(x.Trim().Substring(1), 2)).ToList();
  42.            textBox_Hex.Text = string.Join(", ", numbers.Select(x => "0x" + x.ToString("X2")));
  43.            textBox_Dec.Text = string.Join(", ", numbers.Select(x => x.ToString("00")));
  44.        }
  45.  
  46.        private void button_Bit_Click(object sender, EventArgs e)
  47.        {
  48.            textBox_Bin.Text = Sustituir(textBox_Bin.Text, 12, 1, "1");
  49.        }
  50.  
  51.        private void timer1_Tick(object sender, EventArgs e)
  52.        {
  53.            Convertir();
  54.        }
  55.    }