Advertisement
reymisterio

Convertidor de letras a binario

May 25th, 2015
374
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 11.45 KB | None | 0 0
  1. /* Source Code: Dalmiro Farinello
  2.  * Convertidor de letras a binario SOLO DE A UNA LETRA NO PALABRAS, Y SOLO LAS LETRAS DEL ALFABETO EN minusculas y MAYUSCULAS!!!!
  3.  * GRACIAS espero les guste, lo hice lo mas entendible posible!!!
  4.  */
  5. using System;
  6. using System.Collections.Generic;
  7. using System.ComponentModel;
  8. using System.Data;
  9. using System.Drawing;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. using System.Windows.Forms;
  14.  
  15. namespace traducto
  16. {
  17.     public partial class main : Form
  18.     {
  19.         char[] abc = new char[26], ABC = new char[26];// arrays donde se almacenaran el alfabeto minusculo y mayusculo
  20.         string bin; // variable que contrendra la traduccion
  21.         public main()
  22.         {
  23.             InitializeComponent();
  24.         }
  25.         private void main_Load(object sender, EventArgs e)
  26.         {
  27.             int i1 = 0, i2 = 0;// indice1, indice2
  28.             for (int i = 65; i < 91; i++)// tomando en cuenta la tabla ascii se coloca i en 65 que es el numero de la A y 91 donde termina osea Z
  29.             {
  30.                 ABC[i1] = Convert.ToChar(i);// se convierte cada numero en un nuevo caracter en cada posicion del array
  31.                 i1++;// aumentamos indice1
  32.             }
  33.             for (int i = 97; i <= 122; i++)// aca lo mismo pero esto es para las minisculas
  34.             {
  35.                 abc[i2] = Convert.ToChar(i);// lo mismo que arriba..
  36.                 i2++;// aumentamos indice2
  37.             }
  38.  
  39.         }
  40.  
  41.         private void passTobin_Click(object sender, EventArgs e)// si se hace click blablabla... no hace falta que el boton se llame asi
  42.         {
  43.             bool isInt; // esta variable sera true si un numero tiene decimales o no
  44.             double ascii; // aca estaran los numeros ascii, y se almacenaran los resultados de las diviciones para podes saber si es 0 o 1
  45.             if (textIn.TextLength > 0) // se verifica el largo de la caja donde se encuentra el texto...
  46.             {
  47.                 for (int i = 0; i < 26; i++)// se usa la i para tener todo el abecedario
  48.                 {
  49.                     if (textIn.Text.Contains(ABC[i]))// si el textbox textIn contiene alguna letra mayusculas del array entrame al ciclo...
  50.                     {
  51.                         ascii = Convert.ToDouble(Encoding.ASCII.GetBytes(ABC[i].ToString())[0]) / 2; // como sabemos para saber cuando ay que poner 0 o 1 se divide el numero ascii y si tiene decimales va 1 sino 0.. en esta linea convertimos la letra a ASCII y la dividimos por 2.
  52.                         isInt = ascii % 1 == 0; // si la divicion tiene decimales true sino false
  53.                         if (!isInt)
  54.                         {
  55.                             bin += "1"; // a la variable bin se le da 1 al entrar a este ciclo..
  56.                             ascii = Math.Truncate(ascii); // si la divicion tiene decimales, se la redondea para tener un numero entert
  57.                             while (ascii > 0) // si todabia sigue sin ser 0 la variable ascii empezamos un ciclo while
  58.                             {
  59.                                 ascii = ascii / 2; // de nuevo dividimos
  60.                                 isInt = ascii % 1 == 0; // y si da decimales ya sabemos que pasa..
  61.                                 if (!isInt)
  62.                                 {
  63.                                     bin += "1";// volvemos a asignar
  64.                                     ascii = Math.Truncate(ascii); // volvemos a redondear
  65.                                     if (ascii == 0)// si la variable ascii ya es 0
  66.                                     {
  67.                                         textOut.Text = Invertir(bin); // mostramos el texto en el textbox textOut y la cadena la invertimos ya que la cadena esta dada vuelta, para entender vallan a la funcion Invertir()
  68.                                         bin = ""; // reseteamos bin
  69.                                         break; //paramos ciclo
  70.                                        
  71.                                     }
  72.                                     else
  73.                                     {
  74.                                         continue;// sino continua
  75.                                     }
  76.                                    
  77.                                 }
  78.                                 else
  79.                                 {
  80.                                     bin += "0";// desde aca es lo mismo pero cuando no tiene decimales osea asignamos 0.
  81.                                     ascii = Math.Truncate(ascii);
  82.                                     if (ascii == 0)
  83.                                     {
  84.                                         textOut.Text = Invertir(bin);
  85.                                         bin = "";
  86.                                         break;
  87.                                     }
  88.                                     else
  89.                                     {
  90.                                         continue;
  91.                                     }
  92.                                 }
  93.                             }
  94.                         }
  95.                         else
  96.                         {
  97.                             bin += "0";
  98.                             ascii = Math.Truncate(ascii);
  99.                             while (ascii > 0)
  100.                             {
  101.                                 ascii = ascii / 2;
  102.                                 isInt = ascii % 1 == 0;
  103.                                 if (!isInt)
  104.                                 {
  105.                                     bin += "1";
  106.                                     ascii = Math.Truncate(ascii);
  107.                                     if (ascii == 0)
  108.                                     {
  109.                                         textOut.Text = Invertir(bin);
  110.                                         bin = "";
  111.                                         break;
  112.  
  113.                                     }
  114.                                     else
  115.                                     {
  116.                                         continue;
  117.                                     }
  118.  
  119.                                 }
  120.                                 else
  121.                                 {
  122.                                     bin += "0";
  123.                                     ascii = Math.Truncate(ascii);
  124.                                     if (ascii == 0)
  125.                                     {
  126.                                         textOut.Text = Invertir(bin);
  127.                                         bin = "";
  128.                                         break;
  129.                                     }
  130.                                     else
  131.                                     {
  132.                                         continue;
  133.                                     }
  134.                                 }
  135.                             }
  136.                         }
  137.                     }
  138.                     else if (textIn.Text.Contains(abc[i])) // este es el mismo codigo de antes nada mas que con las letras minusculas
  139.                     {
  140.                         ascii = Convert.ToDouble(Encoding.ASCII.GetBytes(abc[i].ToString())[0]) / 2;
  141.                         isInt = ascii % 1 == 0;
  142.                         if (!isInt)
  143.                         {
  144.                             bin += "1";
  145.                             ascii = Math.Truncate(ascii);
  146.                             while (ascii > 0)
  147.                             {
  148.                                 ascii = ascii / 2;
  149.                                 isInt = ascii % 1 == 0;
  150.                                 if (!isInt)
  151.                                 {
  152.                                     bin += "1";
  153.                                     ascii = Math.Truncate(ascii);
  154.                                     if (ascii == 0)
  155.                                     {
  156.                                         textOut.Text = Invertir(bin);
  157.                                         bin = "";
  158.                                         break;
  159.                                     }
  160.                                     else
  161.                                     {
  162.                                         continue;
  163.                                     }
  164.  
  165.                                 }
  166.                                 else
  167.                                 {
  168.                                     bin += "0";
  169.                                     ascii = Math.Truncate(ascii);
  170.                                     if (ascii == 0)
  171.                                     {
  172.  
  173.                                         textOut.Text = Invertir(bin);
  174.                                         bin = "";
  175.                                         break;
  176.                                     }
  177.                                     else
  178.                                     {
  179.                                         continue;
  180.                                     }
  181.                                 }
  182.                             }
  183.                         }
  184.                         else
  185.                         {
  186.                             bin += "0";
  187.                             ascii = Math.Truncate(ascii);
  188.                             while (ascii > 0)
  189.                             {
  190.                                 ascii = ascii / 2;
  191.                                 isInt = ascii % 1 == 0;
  192.                                 if (!isInt)
  193.                                 {
  194.                                     bin += "1";
  195.                                     ascii = Math.Truncate(ascii);
  196.                                     if (ascii == 0)
  197.                                     {
  198.                                         textOut.Text = Invertir(bin);
  199.                                         bin = "";
  200.                                         break;
  201.  
  202.                                     }
  203.                                     else
  204.                                     {
  205.                                         continue;
  206.                                     }
  207.  
  208.                                 }
  209.                                 else
  210.                                 {
  211.                                     bin += "0";
  212.                                     ascii = Math.Truncate(ascii);
  213.                                     if (ascii == 0)
  214.                                     {
  215.                                         textOut.Text = Invertir(bin);
  216.                                         bin = "";
  217.                                         break;
  218.                                     }
  219.                                     else
  220.                                     {
  221.                                         continue;
  222.                                     }
  223.                                 }
  224.                             }
  225.                         }
  226.                     }
  227.                 }
  228.             }
  229.         }
  230.         public string Invertir(string s)
  231.         {
  232.             string aux = "";
  233.  
  234.             //Recorremos el string en orden inverso
  235.             for (int i = s.Length - 1; i >= 0; i--) // se recorre la cadena y se invierte
  236.             {
  237.                 aux += s[i];//se almacena en la nueva variable
  238.             }
  239.             if (aux.Length == 7)// si la cadena tiene 7 caracteres
  240.             {
  241.                 aux = "0" + aux; // agrega un 0
  242.             }
  243.             else if (aux.Length == 6)// con 8 agregame 2...
  244.             {
  245.                 aux = "00" + aux;
  246.             }
  247.             //retornamos la nueva cadena
  248.             return aux;
  249.         }
  250.  
  251.     }
  252. }
  253. // adios amigos espero les alla gustado!!!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement