Advertisement
f0rkB0mb

Nicos QnD Text2Binary-Converter

Feb 11th, 2013
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.49 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4.  
  5. namespace TextToBin
  6. {
  7.     class Program
  8.     {
  9.         static void Main()
  10.         {
  11.             Console.Title = "Nico aka f0rkB0mb's Text2Bin-Converter";
  12.             /** if you wanna check the indexes of the ASCII-Code **/
  13.             /**
  14.             Console.WriteLine("ASCII:");
  15.             for (int ascii = 33; ascii < 256; ascii++)
  16.             {
  17.                 Console.Write(ascii.ToString() + ") " + Convert.ToChar(ascii) + ", ");
  18.                 if (ascii % 10 == 0)
  19.                     Console.WriteLine();
  20.             }
  21.             **/
  22.             Console.WriteLine("Please insert Textphrase: ");
  23.             string str = Console.ReadLine();
  24.             char[] work = str.ToCharArray();    //push the entered string in a char-array (could also be done within a for-loop)
  25.  
  26.             /** if you wanna check the single chars that was entered **/
  27.             /**
  28.             Console.WriteLine("Your Text as single Chars: ");
  29.             for (int i = 0; i < work.Length; i++)
  30.                 Console.Write("'" + work[i] + "'");
  31.             Console.WriteLine();
  32.             **/
  33.             int[] convert = new int[work.Length];
  34.            
  35.             for (int i = 0; i < work.Length; i++)
  36.             {
  37.                 convert[i] = (int)work[i]; //int.Parse(work[i]) does the same
  38.                 Console.WriteLine("{0}={1}", work[i], decimal2dual(convert[i], 8));
  39.             }
  40.             Console.ReadLine();
  41.         }
  42.  
  43.         private static string decimal2dual(int decNbr, int bitWidth)
  44.         {
  45.             StringBuilder strBuildr = new StringBuilder();
  46.             //we convert it via bitshifting!
  47.             for (int i = 0; i < bitWidth; decNbr = decNbr >> 1, i++)
  48.                 strBuildr.Insert(0, decNbr & 1);
  49.             return strBuildr.ToString();
  50.         }
  51.     }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement