hexidecimal string to integer

 

Hi everyone,

I have been experimenting with strings in mql4 and have stumbled upon a situation I cannot solve:

int myInt1 = 0xffa8b; //myInt1 contains an integer as expected

int myInt2 = StrToInteger("0xffa8b"); //fails

int myInt3 = StrToInteger("\xffa8b"); //fails

Does anyone have thoughts on why 2 and 3 fail? Is there a better/correct way to do this?

TIA,
-Billy

 
Maybe StrToInteger only works with decimals . . .
 

Here is a quite naive solution:


int hexToInteger(string str)
{
   int result=0;
   int power=0;
   for(int pos=StringLen(str)-1; pos>=0; pos--)
   {
      
      int c=StringGetChar( str, pos);
      int value = 0; 
      if(c>='0' && c<='9')
         value=c-'0';
      else if(c>='a' && c<='f')
         value=c-'a'+10;   
      else if(c>='A' && c<='F')
         value=c-'A'+10;      
      
      result += value*MathPow(16.0, power);
      power++;   
   }   
   return(result);
}

Output:

2012.03.09 12:30:15 2009.01.02 13:49 test EURUSDm,M1: 0xaf23=44835



Enjoy it.

 
     result += value*MathPow(16.0, power);
      power++;   
power isn't necessary
    for(int pos=0; pos < StringLen(str); pos++)
    :
        result = result * 16 + value;
 

In pure C you can force varaible casting using (int), I don't know if you can in MQL. Try...

int var = (int) HexVarhere;

 
flaab:

In pure C you can force varaible casting using (int), I don't know if you can in MQL. Try...

int var = (int) HexVarhere;

Except the OP wants to use a hex number as a String.
 
RaptorUK:
Except the OP wants to use a hex number as a String.

and the OP already did the assignment from hex to an int in line 3 of his first post, namely

int myInt1 = 0xffa8b;

No need for a cast.

WHR's improvement to the earlier hexToInteger function should be an adequate solution.

 
RaptorUK:
Except the OP wants to use a hex number as a String.
Oh, my bad. Sorry op.
 

Thanks for all of your comments!

Abstract Mind's solution works perfectly.

In the interest of being clear for other who may have this question, MathPow is, in fact, necessary, as the solution is poping values (place holders), right to left, that correspond with powers of 16.

eg. 0xF2A2 means that we have:

2 of these 16^0 --> 2*1 = 2

10 of these 16^1 --> 10*16 = 160

2 of these 16^2 --> 2*256 = 512

15 of these 16^3 --> 15*4096 = 61440

Finally, 2 + 160 + 512 + 61440 = 62114

I still don't truly understand why the StrToInteger function will not handle this. I suppose I have to accept RaptorUK's hypotheses that StrToInteger can only handle integer ascii characters as it's string argument. This is Interesting to me, since the platform is aware, internally, of Hex numbers, and converts them easily to ints, so long as they are not held as a string. It must have to do with the additional complexity that would be involved with the coding of the StrToInteger function.

I'm going to chalk this up as finished and stop thinking about it now. Thanks again for your comments!

-Billy

 
wboyer:

In the interest of being clear for other who may have this question, MathPow is, in fact, necessary, as the solution is poping values (place holders), right to left, that correspond with powers of 16.

What you missed in WHR's terse (but very neat) answer was that he looped the other way to avoid the "costly" MathPow function.
 
dabbler:
What you missed in WHR's terse (but very neat) answer was that he looped the other way to avoid the "costly" MathPow function.

You are right, I missed the fact that WHR's loop is reversed...that IS a clever improvement!