943,741 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 24900
  • C# RSS
Dec 25th, 2008
0

decimal to hexadecimal

Expand Post »
I want the code of program that convert from "decimal to hexadecimal "and from "hexadecimal to decimal " in console applicathion
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
Egypt Pharaoh is offline Offline
40 posts
since Dec 2008
Dec 25th, 2008
0

Re: decimal to hexadecimal

there is the code I have but I don't know if it was right or wrong from "decimal to hexadecimal"
C# Syntax (Toggle Plain Text)
  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. }
Reputation Points: 10
Solved Threads: 0
Light Poster
Egypt Pharaoh is offline Offline
40 posts
since Dec 2008
Dec 25th, 2008
0

Re: decimal to hexadecimal

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.
Reputation Points: 2035
Solved Threads: 644
Senior Poster
ddanbe is offline Offline
3,736 posts
since Oct 2008
Dec 28th, 2008
0

Re: decimal to hexadecimal

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");
Reputation Points: 11
Solved Threads: 4
Light Poster
hieuuk is offline Offline
44 posts
since Nov 2008
Mar 20th, 2010
0
Re: decimal to hexadecimal
Old thread. Meh,
C# Syntax (Toggle Plain Text)
  1. static string chex8(byte e) // decimal (byte) to hexadecimal conversion
  2. {
  3. string r = "";
  4. string chars = "0123456789ABCDEF";
  5. r += chars[e >> 4];
  6. return r += chars[e &= 0x0F];
  7. }
  8.  
  9. static byte CRAZY_BYTE(string t, int i)
  10. {
  11. if (i == 0) return 0;
  12. throw new Exception(t);
  13. }
  14.  
  15. static byte hbyte8(string e) // hex to decimal (in this case, a byte)
  16. { // Obviously, takes a 2-character hex string
  17. e = e.ToUpper(); //
  18. string msg = "INVALID CHARS";
  19.  
  20. byte[] r = new byte[CRAZY_BYTE("INVALID LENGTH", e.Length ^ 0x02) | 0x02];
  21.  
  22. for (byte i = 0x00; i < 0x02; i++) // [ascii] to [hexadecimal] conversion
  23. {
  24. r[i] = (byte)e[i]; // Get the ascii character
  25. r[i] -= (byte)(((r[i]) >= 0x30) ? 0x30 : CRAZY_BYTE(msg, 0x01));
  26. r[i] -= (byte)((!(r[i] < 0x0A)) ? (r[i] >= 0x11 ? 0x07 : CRAZY_BYTE(msg, 0x01)) : 0x00);
  27. }
  28.  
  29. return r[0x01] |= r[0x00] <<= 0x04; // The moment of truth.
  30. }

Variations, using some of the above functions, for taking 16-bit and 32-bit hex strings (i.e. 4 or 8 character strings of hexadecimal characters) and converting them back to the integer format, e.g.:

C# Syntax (Toggle Plain Text)
  1. static ushort hbyte16(string e)
  2. {
  3. ushort[] r = new ushort[CRAZY_BYTE("INVALID LENGTH", e.Length ^ 0x04) | 0x02];
  4. string[] b = new string[0x02];
  5.  
  6. int index = 0x00;
  7. for(int i = 0x00; i < 0x02; i++)
  8. {
  9. for(int j = 0x00; j < 0x02; j++) b[i] += e[index++];
  10. r[i] = hbyte8(b[i]);
  11. }
  12.  
  13. return r[0x01] |= r[0x00] <<= 0x08;
  14. }
  15.  
  16. static uint hbyte32(string e)
  17. {
  18. uint[] r = new uint[CRAZY_BYTE("INVALID LENGTH", e.Length ^ 0x08) | 0x02];
  19. string[] b = new string[0x02];
  20.  
  21. int index = 0x00;
  22. for(int i = 0x00; i < 0x02; i++)
  23. {
  24. for(int j = 0x00; j < 0x04; j++) b[i] += e[index++];
  25. r[i] = hbyte16(b[i]);
  26. }
  27.  
  28. return r[0x01] |= r[0x00] <<= 0x10;
  29. }
Last edited by NightCrawler03X; Mar 20th, 2010 at 8:15 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
NightCrawler03X is offline Offline
16 posts
since Jan 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC