| | |
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 backup barchart bitmap box broadcast c# check checkbox client clock combobox control conversion csharp custom database databasesearch datagrid datagridview datagridviewcheckbox dataset datetime degrees development draganddrop drawing dynamiccreation encryption enum equation excel file form format formatting forms function gdi+ hospitalmanagementsystems httpwebrequest image index input install interface java label list listbox mandelbrot math microsystems mouseclick mysql namevaluepairs operator path photoshop picturebox pixelinversion post powerpacks programming property radians regex remoting resource restore richtextbox server sleep soap socket sql sqlserver statistics stream string table text textbox thread time timer update usercontrol validation visualstudio wait webbrowser windows winforms working wpf xml






