I want the code of program that convert from "decimal to hexadecimal "and from "hexadecimal to decimal " in console applicathion

Recommended Answers

All 4 Replies

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

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");

Old thread. Meh,

static string chex8(byte e)     // decimal (byte) to hexadecimal conversion
        {
            string r = "";
            string chars = "0123456789ABCDEF";
            r += chars[e >> 4];
            return r += chars[e &= 0x0F];
        }

        static byte CRAZY_BYTE(string t, int i)
        {
            if (i == 0) return 0;
            throw new Exception(t);
        }

        static byte hbyte8(string e)         // hex to decimal (in this case, a byte)
        {                                    // Obviously, takes a 2-character hex string
            e = e.ToUpper(); // 
            string msg = "INVALID CHARS";

            byte[] r = new byte[CRAZY_BYTE("INVALID LENGTH", e.Length ^ 0x02) | 0x02];

            for (byte i = 0x00; i < 0x02; i++)      // [ascii] to [hexadecimal] conversion
            {
                r[i] = (byte)e[i];                  // Get the ascii character
                r[i] -= (byte)(((r[i]) >= 0x30) ? 0x30 : CRAZY_BYTE(msg, 0x01));
                r[i] -= (byte)((!(r[i] < 0x0A)) ? (r[i] >= 0x11 ? 0x07 : CRAZY_BYTE(msg, 0x01)) : 0x00);
            }

            return r[0x01] |= r[0x00] <<= 0x04;     // The moment of truth.
        }

Variations, using some of the above functions, for taking 16-bit and 32-bit hex strings (i.e. 4 or 8 character strings of hexadecimal characters) and converting them back to the integer format, e.g.:

static ushort hbyte16(string e)
        {
            ushort[] r = new ushort[CRAZY_BYTE("INVALID LENGTH", e.Length ^ 0x04) | 0x02];
            string[] b = new string[0x02];

            int index = 0x00;
            for(int i = 0x00; i < 0x02; i++)
            {
                for(int j = 0x00; j < 0x02; j++) b[i] += e[index++];
                r[i] = hbyte8(b[i]);
            }

            return r[0x01] |= r[0x00] <<= 0x08;            
        }

        static uint hbyte32(string e)
        {
            uint[] r = new uint[CRAZY_BYTE("INVALID LENGTH", e.Length ^ 0x08) | 0x02];
            string[] b = new string[0x02];

            int index = 0x00;
            for(int i = 0x00; i < 0x02; i++)
            {
                for(int j = 0x00; j < 0x04; j++) b[i] += e[index++];
                r[i] = hbyte16(b[i]);
            }

            return r[0x01] |= r[0x00] <<= 0x10;
        }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.