| | |
decimal to hexadecimal
Please support our C# advertiser: $4.95 a Month - ASP.NET Web Hosting – Click Here!
![]() |
•
•
Join Date: Dec 2008
Posts: 17
Reputation:
Solved Threads: 0
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)
namespace ConsoleApplication2 { class Program { static void Main(string[] args) { Console.WriteLine("Enter the number that you want to convert to Hexadecimal "); int n = int.Parse(Console.ReadLine()); Console.Write("{0}=",n); ToHexadecimal(n); Console.ReadKey(); } static void ToHexadecimal(int n) { if (n == 0) return; else { int r = n % 16; n =n/ 16; ToHexadecimal(n); switch (r) { case 10: Console.Write("A"); break; case 11: Console.Write("B"); break; case 12: Console.Write("C"); break; case 13: Console.Write("D"); break; case 14: Console.Write("E"); break; case 15: Console.Write("F"); break; default: Console.Write(r); break; } } } } }
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
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
Make love, no war. Cave ab homine unius libri.
Danny
•
•
Join Date: Nov 2008
Posts: 44
Reputation:
Solved Threads: 4
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");
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
My Portfolio/Blog: http://www.hieu.co.uk
![]() |
Similar Threads
- convert Decimal to Hexadecimal system.. (C)
- how to convert decimal value as string input to its hex equivalent (Assembly)
- hexadecimal convertions (C#)
- Decimal to hex conversion with recursion (Java)
- hexadecimal.cpp (C++)
- hop over decimal and straight to business (C++)
- decimal > binary > Oct > Hex (C++)
Other Threads in the C# Forum
- Previous Thread: Sending Bitmap using WCF
- Next Thread: Need Help with Windows Service
| Thread Tools | Search this Thread |
.net access algorithm array asp.net bitmap box broadcast c# check checkbox client combobox control conversion csharp database datagrid datagridview dataset datetime dbconnection decryption degrees design developer draganddrop drawing encryption enum eventhandlers excel file firefox focus form format forms function gdi+ grantorrevokepermissionthroughc#.net hospitalmanagementsystem image input install interface java libraries list loop marshalbyrefobject math mouseclick movingimage mysql netcfsvcutil.exe numeric operator path photoshop php picturebox pixelinversion platform polynomial post programming properties radians read regex remote remoting richtextbox server sleep socket sql statistics string stringformatting study system.servicemodel table tcpclientchannel text textbox thread time timer update usb usercontrol validation visualstudio webbrowser winforms wpf wpfc# xml






