Advertisement
DJ_Zoning

BinToHex

Mar 23rd, 2014
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.53 KB | None | 0 0
  1. using System;
  2. using System.Text;
  3.  
  4. class ConvertBinToHex
  5. {
  6.     static void Main()
  7.     {
  8.         string s = Console.ReadLine();
  9.  
  10.         while (s.Length % 4 != 0)
  11.         {
  12.             s = '0' + s;
  13.         }
  14.         StringBuilder hexRepresent = new StringBuilder();
  15.         for (int i = 0; i < s.Length; i+=4)
  16.         {
  17.             string subStr = s.Substring(i, 4);
  18.             switch (subStr)
  19.             {
  20.                 case "1010": hexRepresent.Append('A'); break;
  21.                 case "1011": hexRepresent.Append('B'); break;
  22.                 case "1100": hexRepresent.Append('C'); break;
  23.                 case "1101": hexRepresent.Append('D'); break;
  24.                 case "1110": hexRepresent.Append('E'); break;
  25.                 case "1111": hexRepresent.Append('F'); break;
  26.                 case "0001": hexRepresent.Append('1'); break;
  27.                 case "0010": hexRepresent.Append('2'); break;
  28.                 case "0011": hexRepresent.Append('3'); break;
  29.                 case "0100": hexRepresent.Append('4'); break;
  30.                 case "0101": hexRepresent.Append('5'); break;
  31.                 case "0110": hexRepresent.Append('6'); break;
  32.                 case "0111": hexRepresent.Append('7'); break;
  33.                 case "1000": hexRepresent.Append('8'); break;
  34.                 case "1001": hexRepresent.Append('9'); break;
  35.                 case "0000": hexRepresent.Append('0'); break;
  36.                 default:
  37.                     break;
  38.             }
  39.         }
  40.         Console.WriteLine(hexRepresent);
  41.     }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement