decimal to hexadecimal
Please support our C# advertiser: DiscountASP.NET – 3 Months Free on C# Web Hosting
![]() |
•
•
Posts: 16
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"
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 7:46 am.
"If you judge people, you have no time to love them." Mother Teresa
Make love, no war. Cave ab homine unius libri.
First rule of debugging: "If you get a different error message, you're making progress."
Danny
Make love, no war. Cave ab homine unius libri.
First rule of debugging: "If you get a different error message, you're making progress."
Danny
•
•
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
Other Threads in the C# Forum
- 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
•
•
•
•
Views: 3314 | Replies: 3 | Currently Viewing: 1 (0 members and 1 guests)






Linear Mode