Geekpedia Programming Tutorials






How do I convert from decimal to hex and hex to decimal?

On Tuesday, July 20th 2004 at 12:00 AM
By Andrew Pociu (View Profile)
*****   (Rated 5 with 5 votes)
Advertisement
More C# Resources
If for any reason you want to convert a decimal number to a hex value or vice versa here's how it's done in C#.

// Store integer 182
int decValue = 182;
// Convert integer 182 as a hex in a string variable
string hexValue = decValue.ToString("X");
// Convert the hex string back to the number
int decAgain = int.Parse(hexValue, System.Globalization.NumberStyles.HexNumber);

In the above example we convert the decimal stored in the int variable named decValue and store it in a string variable named hexValue.
Then we convert the hex value stored as a string in the hexValue variable and store it in the variable named decAgain.
Digg Digg It!     Del.icio.us Del.icio.us     Reddit Reddit     StumbleUpon StumbleIt     Newsvine Newsvine     Furl Furl     BlinkList BlinkList

Rate Rate this Knowledge Base article
Comment Current Comments
by Sara on Monday, October 3rd 2005 at 11:13 AM

Hi, I Would to convert decimal to hex.
If I write
string hexValue = decValue.ToString("X");

in C++.net it's the same??
I think it's so.

Thank you.
Sara

by Andrei Pociu on Monday, October 3rd 2005 at 11:24 AM

Yes, I think so too.
If it doesn't work, try the following:

<i>string hexValue = decValue.ToString(S"X");</i>

by Bijesh on Monday, November 28th 2005 at 05:32 AM

Hi,
You can also try Convert.ToString(value,base) method.
Best Regards
Bijesh

by chiru on Thursday, March 16th 2006 at 07:05 AM

It is ok but some problem is occure anyway once try it

by c on Thursday, June 1st 2006 at 04:36 PM

by on Wednesday, December 13th 2006 at 09:44 AM

saf

by Nishanth on Friday, April 27th 2007 at 04:37 AM

C#
-----
String hexNumber = "000001ae";
int i = Int32.Parse(hexNumber, NumberStyles.HexNumber);
MessageBox.Show(i.ToString());

by Nishanth on Friday, April 27th 2007 at 04:39 AM

C#
-----
C#
-----
String hexNumber = "000001ae";
int i = Int32.Parse(hexNumber, NumberStyles.HexNumber);
MessageBox.Show(i.ToString());


NOTE : Don't forget to include
using System.Globalization;

by Anurada on Wednesday, December 5th 2007 at 08:49 AM

can u please tell me the answer, when the above code is executed

by Nsanate on Wednesday, February 6th 2008 at 12:24 PM

pliz provide me a java source code to convert a decimal to hex and vice versa

by bayan on Monday, February 18th 2008 at 06:59 AM

hi all,
i need code in C++ Language to convert from hexadecimal to decimal
please help
thanks

by badc0ffee on Friday, April 11th 2008 at 02:25 PM

bayan,
In C++, use stringstream:

#include <string>
#include <sstream>
#include <iostream>

using namespace std;

string dec2hex (int num)
{
string str;
ostringstream o;
o << hex << num;
str = o.str();
return str;
}

int hex2dec (string str)
{
int num;
istringstream i(str);
i >> hex >> num;
return num;
}

int main()
{
string str = dec2hex(127);
cout << \"str is \" << str << endl;
int num = hex2dec(\"7f\");
cout << \"num is \" << num << endl;
return 0;
}

by badc0ffee on Friday, April 11th 2008 at 02:28 PM

Trying that again:
#include &lt;string&gt;
#include &lt;sstream&gt;
#include &lt;iostream&gt;

using namespace std;

string dec2hex (int num)
{
string str;
ostringstream o;
o << hex << num;
str = o.str();
return str;
}

int hex2dec (string str)
{
int num;
istringstream i(str);
i >> hex >> num;
return num;
}

int main()
{
string str = dec2hex(127);
cout << \"str is \" << str << endl;
int num = hex2dec(\"7f\");
cout << \"num is \" << num << endl;
return 0;
}

by luis on Wednesday, April 8th 2009 at 01:57 PM

bueno estoy empesando en c# y megustaria que me ayudes con este codigo pero en windos from

by sathiyadasan on Friday, April 17th 2009 at 12:42 AM

I got error for the above c program in windows::

error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'std::string [256]' (or there is no acceptable conversion).

Please reply to this as early as possible.

by Julie on Thursday, June 25th 2009 at 01:08 AM

Hi i need to cnvert the following C# code inyo java. Please help me.

protected iTextSharp.text.Color getColor(string txt)
{
if (txt == null || txt == "") return Color.WHITE;
if (txt.StartsWith("#"))
{
txt = txt.Substring(1);
int res;
res = Int32.Parse(txt, System.Globalization.NumberStyles.HexNumber);
{
return new iTextSharp.text.Color( System.Drawing.Color.FromArgb(res));
}
}
else
{
return new iTextSharp.text.Color(System.Drawing.Color.FromName(txt));
}

return Color.WHITE;
}

by ANIKET on Wednesday, August 26th 2009 at 11:46 AM

guys from the above code for conversion with decimal to hexadecimal there is something missing.And I am not getting it.Please help.


Comment Comment on this Knowledge Base article
Name: Email:
Message:
Knowledge Base Related Knowledge Base Articles
There are no related KB articles.

Comment Related Source Code
There is no related code.

Comment Related Tutorials
There are no related tutorials.

Jobs C# Job Search
My skills include:

Enter a City:

Select a State:


Advanced Search >>
Latest Tech Bargains

Advertisement

Free Magazine Subscriptions

Today's Pictures

Today's Video

Other Resources

Latest Download

Latest Icons