ellapt

T10.5.HexDecToBin

Jan 22nd, 2013
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. using System;
  2.  
  3. class HexDecToBin
  4. {
  5. static void Main()
  6. {
  7. Console.WriteLine("Convert hexadecimal numbers to their binary representation");
  8. Console.WriteLine("The binary representations of the hexadecimal numbers from 0 to 0x40 are: ");
  9. int hexDecNumber = 0;
  10.  
  11. for (int n = 0; n < 0x41; n++) // 0x40 hexadecimal is binary 01000000
  12. {
  13. int binNumber = 0;
  14. hexDecNumber = n;
  15. int temp = hexDecNumber;
  16. byte position = 0;
  17. string printStr = "";
  18. do
  19. {
  20. int bitSet = temp & 1;
  21. binNumber = (bitSet << position) | binNumber;
  22. temp = temp >> 1;
  23. position += 1;
  24. printStr = bitSet + printStr;
  25. } while (temp != 0);
  26. for (int k = 0; k < 8-position; k++)
  27. {
  28. printStr = "0" + printStr;
  29. }
  30. Console.WriteLine("{0:X}\t{1,10}", hexDecNumber, printStr);
  31. }
  32. }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment