RSS Forums RSS

decimal to hexadecimal

Please support our C# advertiser: DiscountASP.NET – 3 Months Free on C# Web Hosting
Reply
Posts: 16
Reputation: Egypt Pharaoh is an unknown quantity at this point 
Solved Threads: 0
Egypt Pharaoh Egypt Pharaoh is offline Offline
Newbie Poster

decimal to hexadecimal

  #1  
Dec 25th, 2008
I want the code of program that convert from "decimal to hexadecimal "and from "hexadecimal to decimal " in console applicathion
AddThis Social Bookmark Button
Reply With Quote  
Posts: 16
Reputation: Egypt Pharaoh is an unknown quantity at this point 
Solved Threads: 0
Egypt Pharaoh Egypt Pharaoh is offline Offline
Newbie Poster

Re: decimal to hexadecimal

  #2  
Dec 25th, 2008
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;
                    
                }

            }
        }

    }
}
Reply With Quote  
Posts: 1,223
Reputation: ddanbe is just really nice ddanbe is just really nice ddanbe is just really nice ddanbe is just really nice 
Solved Threads: 163
ddanbe's Avatar
ddanbe ddanbe is online now Online
Nearly a Posting Virtuoso

Re: decimal to hexadecimal

  #3  
Dec 25th, 2008
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 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
Reply With Quote  
Posts: 44
Reputation: hieuuk is an unknown quantity at this point 
Solved Threads: 4
hieuuk hieuuk is offline Offline
Light Poster

Re: decimal to hexadecimal

  #4  
Dec 28th, 2008
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");
.Net Developer - 3D Game Designer
My Portfolio/Blog: http://www.hieu.co.uk
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.



Views: 3314 | Replies: 3 | Currently Viewing: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 2:45 pm.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC