|
-
July 10th, 2011, 11:50 PM
#1
beginner question about casting
I am trying to make a program that converts a decimal into hexadecimal. It works in the sense that it returns the correct int's but how do I cast my int's that are between 10-15 to A-F as a string. Here is my code:
Code:
static void Main(string[] args)
{
Console.WriteLine("I will convert a number you input into Hexidecimal format.");
Console.Write("Enter your number:");
int yourNum = Int32.Parse(Console.ReadLine());
int hexNum1, hexNum2, hexNum3, hexNum4, hexNum5;
if (yourNum < 65535)
{
hexNum1 = 0;
}
else
hexNum1 = yourNum / 65535;
int rem1 = yourNum % 65535;
if (rem1 < 4096)
{
hexNum2 = 0;
}
else
hexNum2 = rem1 / 4096;
int rem2 = rem1 % 4096;
if (rem2 < 256)
{
hexNum3 = 0;
}
else
hexNum3 = rem2 / 256;
int rem3 = rem2 % 256;
if (rem3 < 16)
{
hexNum4 = 0;
}
else
hexNum4 = rem3 / 16;
int rem4 = rem3 % 16;
if (rem4 < 1)
{
hexNum5 = 0;
}
else
hexNum5 = rem4 / 1;
switch (hexNum1)
{
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
hexNum1.ToString();
break;
case 10:
hexNum1.ToString();
break;
case 11:
hexNum1.ToString();
break;
case 12:
hexNum1.ToString();
break;
case 13:
hexNum1.ToString();
break;
case 14:
hexNum1.ToString();
break;
case 15:
hexNum1.ToString();
break;
default:
hexNum1.ToString();
break;
}
switch (hexNum2)
{
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
hexNum2.ToString();
break;
case 10:
hexNum2.ToString();
break;
case 11:
hexNum2.ToString();
break;
case 12:
hexNum2.ToString();
break;
case 13:
hexNum2.ToString();
break;
case 14:
hexNum2.ToString();
break;
case 15:
hexNum2.ToString();
break;
default:
hexNum2.ToString();
break;
}
switch (hexNum3)
{
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
hexNum3.ToString();
break;
case 10:
hexNum3.ToString();
break;
case 11:
hexNum3.ToString();
break;
case 12:
hexNum3.ToString();
break;
case 13:
hexNum3.ToString();
break;
case 14:
hexNum3.ToString();
break;
case 15:
hexNum3.ToString();
break;
default:
hexNum3.ToString();
break;
}
switch (hexNum4)
{
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
hexNum4.ToString();
break;
case 10:
hexNum4.ToString();
break;
case 11:
hexNum4.ToString();
break;
case 12:
hexNum4.ToString();
break;
case 13:
hexNum4.ToString();
break;
case 14:
hexNum4.ToString();
break;
case 15:
hexNum4.ToString();
break;
default:
hexNum4.ToString();
break;
}
switch (hexNum5)
{
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
hexNum5.ToString();
break;
case 10:
hexNum5.ToString();
break;
case 11:
hexNum5.ToString();
break;
case 12:
hexNum5.ToString();
break;
case 13:
hexNum5.ToString();
break;
case 14:
hexNum5.ToString();
break;
case 15:
hexNum5.ToString();
break;
default:
hexNum5.ToString();
break;
}
Console.WriteLine("Your number in Hex is:{0}, {1}, {2}, {3}, {4}", hexNum1, hexNum2, hexNum3, hexNum4, hexNum5 );
Console.ReadLine();
}
}
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|