Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Text;
- //Write a program to convert binary numbers to hexadecimal numbers (directly)
- namespace practice
- {
- class ConvertToBynary
- {
- static void Main()
- {
- Console.WriteLine("Enter decimal number : ");
- int convert = int.Parse(Console.ReadLine());
- StringBuilder builder = new StringBuilder();
- while (convert > 0)
- {
- if (convert%16 > 9)
- {
- switch (convert%16)
- {
- case 10: builder.Insert(0, 'A'); break;
- case 11: builder.Insert(0, 'B'); break;
- case 12: builder.Insert(0, 'C'); break;
- case 13: builder.Insert(0, 'D'); break;
- case 14: builder.Insert(0, 'E'); break;
- case 15: builder.Insert(0, 'F'); break;
- }
- }
- else
- {
- builder.Insert(0, convert % 16);
- }
- convert /= 16;
- }
- Console.WriteLine("Converted number : \n{0}", builder.ToString());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement