Aller au contenu

Photo

Converting Hex Strings to Integers


  • Veuillez vous connecter pour répondre
5 réponses à ce sujet

#1
Failed.Bard

Failed.Bard
  • Members
  • 774 messages
  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.

#2
Lightfoot8

Lightfoot8
  • Members
  • 2 535 messages
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 .


#3
Failed.Bard

Failed.Bard
  • Members
  • 774 messages
  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.

#4
eeriegeek

eeriegeek
  • Members
  • 47 messages
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;
}



#5
Lightfoot8

Lightfoot8
  • Members
  • 2 535 messages
@eeriegeek,

I like it, much better then mine.

#6
Failed.Bard

Failed.Bard
  • Members
  • 774 messages
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.