Author Topic: Converting Hex Strings to Integers  (Read 366 times)

Legacy_Failed.Bard

  • Hero Member
  • *****
  • Posts: 1409
  • Karma: +0/-0
Converting Hex Strings to Integers
« on: May 06, 2012, 04:46:18 pm »


                 Is there a hard coded function for doing this?  I've been checking valid targets based on bit flags, but I don't like how inefficient the string comparison method of converting them is.

  The script I wrote for it is this:

// Converts the 0x?? hex string from MetamagicType and TargetType strings
// to usable integers.
int HexStringToInt (string sHex)
{
 sHex = GetStringLowerCase (GetStringRight (sHex, 2));
 int i;

 for (i = 0; i < 256; i++)
      {
        if (GetStringLowerCase (GetStringRight (IntToHexString (i), 2)) == sHex) return i;
      }
 return 0;
}

It works, but it's 256 potential int to hex string conversions and comparisons.  If there's a better way I'd like to know about it.  The script isn't called often, but I don't like wasteful routines.
               
               

               
            

Legacy_Lightfoot8

  • Hero Member
  • *****
  • Posts: 4797
  • Karma: +0/-0
Converting Hex Strings to Integers
« Reply #1 on: May 06, 2012, 06:12:51 pm »


               Compiled but untested.

// Converts the 0x?? hex string from MetamagicType and TargetType strings
// to usable integers.
int HexStringToInt (string sHex)
{
 string HexDigits = "0123456789abcdef";
 sHex = GetStringLowerCase (sHex);
 
int nCount = GetStringLength(sHex);
 int nDigitPosValue = 1;
 int nValue =0;
 int nDigitValue;
 while (nCount)
 {
   nCount--;
   nDigitValue = FindSubString (HexDigits,GetSubString(sHex,nCount,1));
   if (nDigitValue != -1)
   {
     nValue += nDigitValue * nDigitPosValue;
     nDigitPosValue *= 16;
   }
   else break; // We hit an invalid character, Most likely the x.
 }
 return nValue;
}

EDIT: spotted my rookie mistake even without testing.  Hopefully it is good now.
               
               

               


                     Modifié par Lightfoot8, 06 mai 2012 - 05:20 .
                     
                  


            

Legacy_Failed.Bard

  • Hero Member
  • *****
  • Posts: 1409
  • Karma: +0/-0
Converting Hex Strings to Integers
« Reply #2 on: May 06, 2012, 09:08:37 pm »


                 Thank you.  I didn't like the script as I was writing it, but couldn't think of a better way to handle it.  I tested return values against mine, at 0x0a, 02a, 0x3a, 0x3f, and 0xff, and both returned the proper values.

  It was the inefficiency of mine I didn't like, and from the comparative test I did against yours, I was right to not like it.

0x2f                        times  ms
test_hex2int_lf         1000        202 
test_hex2int_fb        1000       1138

0xff                          times  ms
test_hex2int_lf          1000        391
test_hex2int_fb         1000        4896

Yes, that's almost 5 full seconds for 1000 runs with mine.  I knew it was horrible.  I'll have to start using FindSubString more for that sort of thing.  I just never even thought of it.
               
               

               
            

Legacy_eeriegeek

  • Jr. Member
  • **
  • Posts: 75
  • Karma: +0/-0
Converting Hex Strings to Integers
« Reply #3 on: May 06, 2012, 11:07:21 pm »


               Just for fun here's another variant with one less multiply in the loop.


const string sHexDigits = "0123456789abcdef";
int HexStringToInt (string sHex)
{
    int nVal = 0;
    string sDig = GetStringLowerCase(sHex);
    int nLen = GetStringLength(sDig);
    int nIdx = 2;
    while (nIdx < nLen)
    {
        nVal = (nVal << 4) + FindSubString(sHexDigits,GetSubString(sDig,nIdx++,1));
    }
    return nVal;
}


               
               

               
            

Legacy_Lightfoot8

  • Hero Member
  • *****
  • Posts: 4797
  • Karma: +0/-0
Converting Hex Strings to Integers
« Reply #4 on: May 06, 2012, 11:22:16 pm »


               @eeriegeek,

I like it, much better then mine.
               
               

               
            

Legacy_Failed.Bard

  • Hero Member
  • *****
  • Posts: 1409
  • Karma: +0/-0
Converting Hex Strings to Integers
« Reply #5 on: May 07, 2012, 12:33:18 am »


               Very nice, short and precise, and more efficient than Lightfoot's by a third as well.  I was expecting to see one better way of doing it, and that's two now.