-
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();
}
}
}
-
July 11th, 2011, 09:24 AM
#2
Re: beginner question about casting
Isn't there a Hex function in C#?
I've never looked in C# but I know VB has one. I would think C# does as well.
Always use [code][/code] tags when posting code.
-
July 11th, 2011, 12:34 PM
#3
Re: beginner question about casting
Well, the question is a bit erroneous as you cannot cast a string to an int at all; you can only convert it. Don't roll your own implementation here, just use the overload of int.Parse() that takes a NumberStyles argument; http://msdn.microsoft.com/en-us/libr...=vs.71%29.aspx
Code:
string hex = "0x01";
int result = int.Parse( hex, NumebrStyles.AllowHexSpecifier );
I don't know how you intend on representing decimal (fractional) numbers in hex. It is possible of course, but hex is almost always used to represent integers, so your request makes little sense.
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
|