Advertisement
cortez

BinaryToHexadecimal

Jan 29th, 2013
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.26 KB | None | 0 0
  1. using System;
  2.  
  3. class binToHexDirectly
  4. {
  5.     static string ToHex(string digitStr)
  6.     {
  7.         string output = "";
  8.  
  9.         switch (digitStr)
  10.         {
  11.             case "0000":
  12.                 output = "0";
  13.                 break;
  14.             case "0001":
  15.                 output = "1";
  16.                 break;
  17.             case "0010":
  18.                 output = "2";
  19.                 break;
  20.             case "0011":
  21.                 output = "3";
  22.                 break;
  23.             case "0100":
  24.                 output = "4";
  25.                 break;
  26.             case "0101":
  27.                 output = "5";
  28.                 break;
  29.             case "0110":
  30.                 output = "6";
  31.                 break;
  32.             case "0111":
  33.                 output = "7";
  34.                 break;
  35.             case "1000":
  36.                 output = "8";
  37.                 break;
  38.             case "1001":
  39.                 output = "9";
  40.                 break;
  41.             case "1010":
  42.                 output = "10";
  43.                 break;
  44.             case "1011":
  45.                 output = "11";
  46.                 break;
  47.             case "1100":
  48.                 output = "12";
  49.                 break;
  50.             case "1101":
  51.                 output = "13";
  52.                 break;
  53.             case "1110":
  54.                 output = "14";
  55.                 break;
  56.             case "1111":
  57.                 output = "15";
  58.                 break;
  59.         }
  60.  
  61.         return output;
  62.     }
  63.     static void Main()
  64.     {
  65.         Console.WriteLine("enter a Binary number to see its Hexadecimal representation:");
  66.         string number = Console.ReadLine();
  67.         char[] tmpChar = number.ToCharArray();// Converting to char array so that every digit is sepparated from the others.
  68.         string[] tmpStr = new string[tmpChar.Length];//Converting to string for ease of control over every digit alone.
  69.         string tmp = "";
  70.         string[] output = new string[tmpStr.Length];
  71.         string result = "";
  72.         int j = 0;
  73.         int m = 0;
  74.  
  75.         for (int i = 0; i < tmpChar.Length; i++)
  76.         {
  77.             if(j == 4)
  78.             {
  79.                 char[] rev1 = tmp.ToCharArray();
  80.                 tmp = "";
  81.                 Array.Reverse(rev1);
  82.                 for (int k = 0; k < rev1.Length; k++)
  83.                 {
  84.                     tmp += rev1[k];
  85.                 }
  86.                 tmpStr[m] = tmp;
  87.                 m++;
  88.                 j = 0;
  89.                 tmp = "";
  90.             }
  91.             tmp += tmpChar[tmpChar.Length - i - 1].ToString();
  92.             j++;
  93.             if (i == tmpChar.Length - 1 && j < 4)
  94.             {
  95.                 if (j == 1)
  96.                 {
  97.                     tmp += "000";
  98.                 }
  99.                 else if (j == 2)
  100.                 {
  101.                     tmp += "00";
  102.                 }
  103.                 else if (j == 3)
  104.                 {
  105.                     tmp += "0";
  106.                 }
  107.                 char[] rev = tmp.ToCharArray();
  108.                 tmp = "";
  109.                 Array.Reverse(rev);
  110.                 for (int s = 0; s < rev.Length; s++)
  111.                 {
  112.                     tmp += rev[s];
  113.                 }
  114.                 tmpStr[m] = tmp;
  115.                 tmp = "";
  116.             }
  117.         }
  118.         for (int i = 0; i < tmpStr.Length; i++)
  119.         {
  120.             output[i] = ToHex(tmpStr[tmpStr.Length - i - 1]);
  121.             //Solving problem with Letters!
  122.             switch (output[i])
  123.             {
  124.                 case "10":
  125.                     output[i] = "A";
  126.                     break;
  127.                 case "11":
  128.                     output[i] = "B";
  129.                     break;
  130.                 case "12":
  131.                     output[i] = "C";
  132.                     break;
  133.                 case "13":
  134.                     output[i] = "D";
  135.                     break;
  136.                 case "14":
  137.                     output[i] = "E";
  138.                     break;
  139.                 case "15":
  140.                     output[i] = "F";
  141.                     break;
  142.                 default:
  143.                     break;
  144.             }
  145.         }
  146.         for (int i = 0; i < output.Length; i++)
  147.         {
  148.             result += output[i];
  149.         }
  150.  
  151.         Console.WriteLine("\nTo Hexadecimal: {0}", result);
  152.     }
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement