Windows Develop Bookmark and Share   
 index > Windows Forms General > How can I convert a decimal to hexadecimal.
 

How can I convert a decimal to hexadecimal.

How can I convert a decimal to hexadecimal.

I want to convert an integer (30) to hex (0x1E or the format of 1s and 0s)

gifuran  Monday, February 05, 2007 12:13 PM
Hexadecimal representation of integral values is only relevant if you convert their value to a string. For example:

int value = 30;
string hex = "0x" + value.ToString("X2");

There is no built-in format specifier for binary formats.
nobugz  Monday, February 05, 2007 1:15 PM

try this code,

public static string Ten2Hex(string ten)
{
ulong tenValue = Convert.ToUInt64(ten);
ulong divValue,resValue;
string hex = "";
do
{
divValue = (ulong)Math.Floor(tenValue/16);
resValue = tenValue%16;
hex = tenValue2Char(resValue) + hex;
tenValue = divValue;
}
while (tenValue >= 16);
if (tenValue != 0)
hex = tenValue2Char(tenValue) + hex;
return hex;
}

public static string tenValue2Char(ulong ten)
{
switch(ten)
{
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
return ten.ToString();
case 10:
return "A";
case 11:
return "B";
case 12:
return "C";
case 13:
return "D";
case 14:
return "E";
case 15:
return "F";
default:
return "";
}
}

Gavin Jin - MSFT  Tuesday, February 06, 2007 3:51 AM

You can use google to search for other answers

Custom Search

More Threads

• IHTMLElementRender - Making Thumbnails render DrawToDC Used to work, FLASH and other Stuff not showing
• Assertion Failed! Handle has not been created.
• Problem with treeView.NodeMouseClick Event
• unzip & zip w/ SharpZipLib... plus ListBox Q?
• datagridview
• create, delete,save,write,save,save as .txt file
• Highlight Texts in a webbrowser
• Accessing data from another form
• archiving feature in .net
• Loading Controls based on contents of a database