ellapt

T10.6.BinToHexDec

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