Go Back   RunUO - Ultima Online Emulation > Developer's Corner > Programming > C#

C# C# Discussion

Reply
 
Thread Tools Display Modes
Old 04-27-2006, 07:30 PM   #1 (permalink)
 
Join Date: Oct 2005
Age: 27
Posts: 2
Default Converting hex Numbers to Decimal

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;
}
mgardner323 is offline   Reply With Quote
Old 04-28-2006, 09:20 AM   #2 (permalink)
Forum Expert
 
Join Date: Sep 2002
Age: 23
Posts: 1,472
Default

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);
        }
Ravatar is offline   Reply With Quote
Old 04-28-2006, 05:11 PM   #3 (permalink)
Forum Newbie
 
Join Date: Nov 2002
Posts: 89
Default small adjustment

Quote:
Originally Posted by Ravatar
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);
        }
Not a biggie, but the above code snippet I believe makes two assumptions (I do not profess to be versed in C#).
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.
punt59 is offline   Reply With Quote
Old 04-28-2006, 09:13 PM   #4 (permalink)
Forum Expert
 
Join Date: Sep 2002
Age: 23
Posts: 1,472
Default

Quote:
Originally Posted by punt59
Not a biggie, but the above code snippet I believe makes two assumptions (I do not profess to be versed in C#).
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.
Well... if you were working with data that you could not verify as being valid ahead of time..

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);
        }
That is a little less trusting of the input. It worked with the following cases:

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
If anyone tries to pass something even worse through this method, they're probably better off receiving an exception.

Last edited by Ravatar; 04-28-2006 at 09:16 PM.
Ravatar is offline   Reply With Quote
Old 04-28-2006, 09:16 PM   #5 (permalink)
Forum Newbie
 
Join Date: Nov 2002
Posts: 89
Default

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.
punt59 is offline   Reply With Quote
Old 04-29-2006, 10:56 PM   #6 (permalink)
Forum Expert
 
Join Date: Sep 2002
Age: 23
Posts: 1,472
Default

Well, I coulda swore in Visual Studio 7 it would refuse "0X...", however Visual Studio 8 seems to be okay with it. As far as its validity, either way will work although 99.9% of the time it's represented "0x".
Ravatar is offline   Reply With Quote
Old 04-30-2006, 12:58 AM   #7 (permalink)
Master of the Internet
 
Lord_Greywolf's Avatar
 
Join Date: Dec 2005
Posts: 6,028
Send a message via Yahoo to Lord_Greywolf
Default

me - i just open up windows calculater - set it for scientific mode, click on hex and plug in the hex nember and then hit the decimal button and covert it and place it in
Lord_Greywolf is offline   Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off



Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0 RC5