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