Geekpedia Tutorials Home

Building a C# Chat Client and Server

Building a C# Chat Client and ServerA step by step tutorial teaching you how to create your own chat client and chat server easily in C#, for local networks or the Internet.

in C# Programming Tutorials

Getting Hard Drive Information

Getting Hard Drive InformationA C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.

in C# Programming Tutorials

UPS Shipping Calculator

UPS Shipping CalculatorThis tutorial will teach you how to calculate the shipping cost based on the weight, height, length and depth of the box, the distance and the UPS service type.

in PHP Programming Tutorials

Create Your Own Rich Text Editor

Create Your Own Rich Text EditorCreating a Rich Text Editor using JavaScript is easier to do than you might think, thanks to the support of modern browsers; this tutorial will walk you through it.

in JavaScript Programming Tutorials
Search
Tutorials
Programming Tutorials
IT Jobs
From CareerBuilder

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 4.5 with 24 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.

by Ecstatic Coder on Saturday, March 20th 2010 at 02:36 PM

Old thread, but meh.

[code]

static string chex(byte e) // Convert a byte to a string representing that byte in hexadecimal
{
string r = "";
string chars = "0123456789ABCDEF";
r = chars[e >> 4];
return r = chars[e

by Ecstatic Coder on Saturday, March 20th 2010 at 02:37 PM

Huh, some text got cut off... here is the code

static string chex(byte e) // Convert a byte to a string representing that byte in hexadecimal
{
string r = "";
string chars = "0123456789ABCDEF";
r = chars[e >> 4];
return r = chars[e

by David on Monday, March 29th 2010 at 04:00 PM

This is not c#, but this is live :)
When I need to convert some value right away, I use this <a href="http://www.stringfunction.com/hex-decimal.html">hex to decimal converter</a>
Pretty cool!
David

by David on Monday, March 29th 2010 at 04:01 PM

Here is the link http://www.stringfunction.com/hex-decimal.html
David

by Frank on Thursday, April 8th 2010 at 06:26 AM

I am surffing for a while in internet to solve decimal conversion problem finally i found a website which is very effective
to solve decimal to fraction conversion problem.. i am sharing you guys my discovery here is a URL of the website http://www.decimaltofraction.com

by Bilal on Thursday, September 16th 2010 at 04:10 PM

Helloo

by rw on Tuesday, September 21st 2010 at 06:40 AM

test

by CaptainHare on Monday, October 11th 2010 at 04:15 AM

i noticed a simple problem when converting. sometimes it will only give you one char.

here is a fix:

public string RGBtoHEX (byte r, byte g, byte b)
{
string hr = r.ToString("X");
string hg = g.ToString("X");
string hb = b.ToString("X");
hr = (hr.Length == 1) ? "0" hr : hr;
hg = (hg.Length == 1) ? "0" hg : hg;
hb = (hb.Length == 1) ? "0" hb : hb;
return "#" hr hg hb;
}

Console.WriteLine(RGBtoHEX(254,67,11));

by CaptainHare on Monday, October 11th 2010 at 04:20 AM

it messed up my code.
when you see this: PP
add a (plus sign)

public string RGBtoHEX (byte r, byte g, byte b)
{
string hr = r.ToString("X");
string hg = g.ToString("X");
string hb = b.ToString("X");
hr = (hr.Length == 1) ? "0" PP hr : hr;
hg = (hg.Length == 1) ? "0" PP hg : hg;
hb = (hb.Length == 1) ? "0" PP hb : hb;
return "#" PP hr PP hg PP hb;
}

Console.WriteLine(RGBtoHEX(254,67,11));

by Frank on Thursday, October 14th 2010 at 05:49 PM

Hello, C# code here.

"decimal" to hex (using bytes):
[code]
string bhex(byte x)
{
string hex = "0123456789ABCDEF";
return hex[x>>4] "" hex[x

by dsfsd on Thursday, October 14th 2010 at 05:51 PM

Hello, C# code here. WTF my last post had stuff cut out. Try again..

"decimal" to hex (using bytes):

string bhex(byte x)
{
string hex = "0123456789ABCDEF";
return hex[x>>4] "" hex[x

by sang on Thursday, November 18th 2010 at 11:01 AM

thanks

by asd on Thursday, December 2nd 2010 at 05:56 AM

fuck yo

by Ecstatic Coder on Saturday, December 11th 2010 at 12:25 PM

input: unsigned byte cInput
byte cTemp = cInput;
cTemp >>= 4;
string sOrgOut = "0123456789ABCDEF"[cTemp];
cInput

by Ecstatic Coder on Saturday, December 11th 2010 at 12:26 PM

Hmm.. email for reply

Tried again months later. Still nothing.

This forum is a bit... shit. Bugs are supposed to be fixed, not kept.

by Frank on Sunday, January 2nd 2011 at 02:32 AM

How to write a dec-hex-octal dump program in c ?
Anyone can assist?

Many Thanks.

by mm on Monday, July 18th 2011 at 02:24 AM

i have an error in unassigned local variable ,but i already assigned it with appropriate variable.what shall i do?

by mm on Monday, July 18th 2011 at 02:24 AM

i have an error in unassigned local variable ,but i already assigned it with appropriate variable.what shall i do?

by mm on Monday, July 18th 2011 at 02:24 AM

i have an error in unassigned local variable ,but i already assigned it with appropriate variable.what shall i do?

by mm on Monday, July 18th 2011 at 02:25 AM

i have an error in unassigned local variable ,but i already assigned it with appropriate variable.what shall i do?

by jaideep sajja on Sunday, September 4th 2011 at 02:11 AM

it is better to give an example of conversion from binary form to decimal form so that it can be understandable to each and every one

by nipz on Sunday, October 16th 2011 at 10:37 PM

may i favor plz give me a codes about how to convert a hexadecimal to decimal...please...thanks

by nipz on Sunday, October 16th 2011 at 10:38 PM

may i favor plz give me a codes about how to convert a hexadecimal to decimal...please...thanks

by nipz on Sunday, October 16th 2011 at 10:38 PM

may i favor plz give me a codes about how to convert a hexadecimal to decimal...please...thanks

by nipz on Sunday, October 16th 2011 at 10:38 PM

may i favor plz give me a codes about how to convert a hexadecimal to decimal...please...thanks

by web 2.0 linkwheels on Tuesday, December 20th 2011 at 08:15 PM

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

by credit loans on Tuesday, December 27th 2011 at 03:03 PM

People deserve very good life time and loans or just student loan would make it better. Just because freedom relies on money state.

by theva on Tuesday, January 3rd 2012 at 10:20 AM

Write a DEC-HEX-OCTAL dump program, where you read an integer value
from keyboard, and display the value as a hexadecimal number (base 16) and
as a octal number (base 8) in the main routine.
Use the following function prototypes to access converted numbers:
void dec2hex(int

by mortgage loans on Tuesday, January 24th 2012 at 08:11 PM

When you're in the corner and have got no money to get out from that point, you will require to take the loan. Just because it will help you for sure. I take student loan every year and feel myself fine just because of this.

by www.ForeclosureHelp.org on Friday, February 24th 2012 at 04:57 PM

Your blog is very informative. I really appreciate your hardwork. Thanks You for such good information. Keep up good work !!

by EducationRequirements on Sunday, March 4th 2012 at 11:51 PM

Thanks a lot for all the helpful theory.

by AllAboutDivorce.com on Monday, March 5th 2012 at 01:50 PM

Hola. Excellent blog. Thanks
<a href="http://www.allaboutdivorce.net">AllAboutDivorce.com</a>

by StartCleaningService.com on Monday, March 12th 2012 at 06:37 AM

Your blog is very informative. I really appreciate your hardwork. Thanks You for such good information. Keep up good work !!
<a href="http://www.startcleaningservice.com">StartCleaningService.com</a>

by StartCleaningService.com on Monday, March 12th 2012 at 06:37 AM

Your blog is very informative. I really appreciate your hardwork. Thanks You for such good information. Keep up good work !!
<a href="http://www.startcleaningservice.com">StartCleaningService.com</a>

by EducationRequirements.net on Sunday, April 8th 2012 at 05:31 AM

Your blog is very informative. I really appreciate your hardwork. Thanks You for such good information. Keep up good work !!

by here on Tuesday, April 24th 2012 at 04:50 AM

Your blog is very informative. I really appreciate your hardwork. Thanks You for such good information. Keep up good work !!

by cecile on Tuesday, October 14th 2014 at 09:41 PM

hi.please help me code this one.

hexadecimal to decimal
hexadecimal to octal
the result of the conversion must be used to ouput the following pattern:12345
23451
34*12
45123
54321
-cecile

by cecile on Tuesday, October 14th 2014 at 09:42 PM

hi.please help me code this one using c#.

hexadecimal to decimal
hexadecimal to octal
the result of the conversion must be used to ouput the following pattern:12345
23451
34*12
45123
54321
-cecile

by cecile on Tuesday, October 14th 2014 at 09:42 PM

hi.please help me code this one using c#.

hexadecimal to decimal
hexadecimal to octal
the result of the conversion must be used to ouput the following pattern:12345
23451
34*12
45123
54321
-cecile

by cecile on Tuesday, October 14th 2014 at 09:43 PM

hi.please help me code this one using c#.

hexadecimal to decimal
hexadecimal to octal
the result of the conversion must be used to ouput the following pattern:12345
23451
34*12
45123
54321
-cecile


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 >>
Ads

From the creators of Geekpedia, a revolutionary new coupon website!

BargainEZ has coupons codes, printable coupons, bargains and it is the leading source of Passbook coupons for iPhone and iPod touch devices.

Coupons
Discover Geekpedia
Other Resources