| | |
decimal to hexadecimal
Please support our C# advertiser: Intel Parallel Studio Home
![]() |
•
•
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 barchart bitmap box broadcast c# check checkbox client combobox control conversion csharp custom customactiondata database datagrid datagridview dataset datastructure date/time datetime datetimepicker degrees development dll draganddrop drawing encryption enum event excel file filename files form format forms function gdi+ gis gtk hash image index input install java label list listbox mandelbrot math mouseclick mysql operator outlook2003 path photoshop picturebox pixelinversion pixelminversion post programming radians regex remoting richtextbox safari server sleep snooze socket sql statistics stream string table tables tcp text textbox thread time timer update usercontrol usercontrols validation visualstudio webbrowser webcam wia windows winforms wpf xml






