decimal to hexadecimal

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Dec 2008
Posts: 17
Reputation: Egypt Pharaoh is an unknown quantity at this point 
Solved Threads: 0
Egypt Pharaoh Egypt Pharaoh is offline Offline
Newbie Poster

decimal to hexadecimal

 
0
  #1
Dec 25th, 2008
I want the code of program that convert from "decimal to hexadecimal "and from "hexadecimal to decimal " in console applicathion
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 17
Reputation: Egypt Pharaoh is an unknown quantity at this point 
Solved Threads: 0
Egypt Pharaoh Egypt Pharaoh is offline Offline
Newbie Poster

Re: decimal to hexadecimal

 
0
  #2
Dec 25th, 2008
there is the code I have but I don't know if it was right or wrong from "decimal to hexadecimal"
  1. namespace ConsoleApplication2
  2. {
  3. class Program
  4. {
  5. static void Main(string[] args)
  6. {
  7.  
  8. Console.WriteLine("Enter the number that you want to convert to Hexadecimal ");
  9. int n = int.Parse(Console.ReadLine());
  10.  
  11. Console.Write("{0}=",n);
  12. ToHexadecimal(n);
  13. Console.ReadKey();
  14.  
  15. }
  16. static void ToHexadecimal(int n)
  17. {
  18. if (n == 0)
  19. return;
  20. else
  21. {
  22. int r = n % 16;
  23. n =n/ 16;
  24. ToHexadecimal(n);
  25.  
  26.  
  27. switch (r)
  28. {
  29. case 10:
  30. Console.Write("A");
  31. break;
  32. case 11:
  33. Console.Write("B");
  34. break;
  35. case 12:
  36. Console.Write("C");
  37. break;
  38. case 13:
  39. Console.Write("D");
  40. break;
  41. case 14:
  42. Console.Write("E");
  43. break;
  44. case 15:
  45. Console.Write("F");
  46. break;
  47. default:
  48. Console.Write(r);
  49. break;
  50.  
  51. }
  52.  
  53. }
  54. }
  55.  
  56. }
  57. }
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,902
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 273
ddanbe's Avatar
ddanbe ddanbe is online now Online
Posting Virtuoso

Re: decimal to hexadecimal

 
0
  #3
Dec 25th, 2008
Off course you can always save you the trouble of doing it yourself and use format specifiers like this:
int value = 32767;
Console.WriteLine("{0,10:G}: {0,10 : X}", value);
Which will output:

32767: 7FFF
Last edited by ddanbe; Dec 25th, 2008 at 8:46 am.
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 44
Reputation: hieuuk is an unknown quantity at this point 
Solved Threads: 4
hieuuk hieuuk is offline Offline
Light Poster

Re: decimal to hexadecimal

 
0
  #4
Dec 28th, 2008
this also does the trick to convert from hex -> number:
int.Parse(Hex, System.Globalization.NumberStyles.HexNumber);
for bigger value
long.Parse(Hex, System.Globalization.NumberStyles.HexNumber);
other way round
string hex = Value.ToString("x");
.Net Developer - 3D Game Designer
My Portfolio/Blog: http://www.hieu.co.uk
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC