|
||
|
|
#1 (permalink) |
|
Join Date: Oct 2005
Age: 27
Posts: 2
|
I want to convert hex numbers to Decimal numbers can someone give me an example on how to do this:
I have it read a MUL for the gumps and it reads everything into as a hex number after I place the gump i can tell it to show the script pretty much and I need to convert the hex number to Decimal. heres what happens when you have it show the script: public override string ToString() { return "GumpPic "+Convert.ToString(ID,16).ToUpper()+" "+_x+" "+_y; } |
|
|
|
|
|
#2 (permalink) |
|
Forum Expert
Join Date: Sep 2002
Age: 23
Posts: 1,472
|
Code:
public static int ConvertHexToDecimal(string hexNumber)
{
if (hexNumber.IndexOf("0x") != -1)
hexNumber = hexNumber.Substring(2, hexNumber.Length - 2);
return Int32.Parse(hexNumber, System.Globalization.NumberStyles.AllowHexSpecifier);
}
|
|
|
|
|
|
#3 (permalink) | |
|
Forum Newbie
Join Date: Nov 2002
Posts: 89
|
Quote:
1. That the 0x occurs at the very beggining of the string. That there is no other whitespace in the string leading up to it. To address, one would adjust the Substring() call to use the index returned by the IndexOf() call. 2. It also assumes that one will never enter 0X instead of 0x. These assumptions are probably valid 99% of the time, so I am sure one is probably making fine making these assumptions. |
|
|
|
|
|
|
#4 (permalink) | |
|
Forum Expert
Join Date: Sep 2002
Age: 23
Posts: 1,472
|
Quote:
Code:
public static int ConvertHexToDecimal(string hexNumber)
{
hexNumber = hexNumber.Trim().ToLower();
int intStart = hexNumber.IndexOf("0x");
if (intStart > -1)
{
intStart += 2;
hexNumber = hexNumber.Substring(intStart, hexNumber.Length - intStart);
}
return Int32.Parse(hexNumber, System.Globalization.NumberStyles.AllowHexSpecifier);
}
Code:
Pre-Conversion: "0X411901" Post-Conversion: 4266241 Pre-Conversion: " 0x7F00FF00" Post-Conversion: 2130771712 Pre-Conversion: "sometext 0X04" Post-Conversion: 4 Pre-Conversion: " 0x12345678" Post-Conversion: 305419896 Last edited by Ravatar; 04-28-2006 at 09:16 PM. |
|
|
|
|
|
|
#5 (permalink) |
|
Forum Newbie
Join Date: Nov 2002
Posts: 89
|
Hmm, I thought 0x and 0X where both valid (at least in most of my C++ data reading days). One can never tell what a user will do.
Never knew it wasn't technically valid! Good to know. Thanks for that tidbit! A word of caution though, if deaing with user generated data, one may run into it. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|