Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Text;
- class ConvertBinToHex
- {
- static void Main()
- {
- string s = Console.ReadLine();
- while (s.Length % 4 != 0)
- {
- s = '0' + s;
- }
- StringBuilder hexRepresent = new StringBuilder();
- for (int i = 0; i < s.Length; i+=4)
- {
- string subStr = s.Substring(i, 4);
- switch (subStr)
- {
- case "1010": hexRepresent.Append('A'); break;
- case "1011": hexRepresent.Append('B'); break;
- case "1100": hexRepresent.Append('C'); break;
- case "1101": hexRepresent.Append('D'); break;
- case "1110": hexRepresent.Append('E'); break;
- case "1111": hexRepresent.Append('F'); break;
- case "0001": hexRepresent.Append('1'); break;
- case "0010": hexRepresent.Append('2'); break;
- case "0011": hexRepresent.Append('3'); break;
- case "0100": hexRepresent.Append('4'); break;
- case "0101": hexRepresent.Append('5'); break;
- case "0110": hexRepresent.Append('6'); break;
- case "0111": hexRepresent.Append('7'); break;
- case "1000": hexRepresent.Append('8'); break;
- case "1001": hexRepresent.Append('9'); break;
- case "0000": hexRepresent.Append('0'); break;
- default:
- break;
- }
- }
- Console.WriteLine(hexRepresent);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement