Decimal to Binary

 
Is there a function in MQL4 that converts a decimal value into its binary representation (for example, in string format of 0's and 1's)?
 
Rasoul: Is there a function in MQL4 that converts a decimal value into its binary representation (for example, in string format of 0's and 1's)?
  1. Did you look at all the available functions and find any? It doesn't exist.
  2. Did you attempt to code one? Didn't even try, did you? learn to code it, or pay someone. We're not going to code it FOR you. We are willing to HELP you when you post your attempt (using SRC) and the nature of your problem.
  3. Just typed, not compiled, not tested.
    string IntToBinary(int i){
       if(i == 0) return "0";
       if(i < 0) return "-" + IntToBinary(-i);
       string out = "";
       for(; i != 0; i /= 2) out = string(i % 2) + out;
       return out;
    }
    Just typed, not compiled, not tested.
    as that so hard?
 
WHRoeder:
  1. Did you look at all the available functions and find any? It doesn't exist.
  2. Did you attempt to code one? Didn't even try, did you? learn to code it, or pay someone. We're not going to code it FOR you. We are willing to HELP you when you post your attempt (using SRC) and the nature of your problem.
  3. Just typed, not compiled, not tested.Just typed, not compiled, not tested.
    as that so hard?

1. I looked at all the functions in MQL4 language that I could find in the website, but none of them performs decimal to binary conversion.

2. No, I didn't. Because I wanted to make sure that there is no such standard function in MQL4.

3. Thanks for the code, I'll compile and test it. I really didn't want to bother someone to code it for me, I know how to code. I just asked to know if somebody is aware of existence of such function in MQL4 or not.

 
void OnTick(){
  Comment(
    string(BinaryToInt("0000"))+"==0=="+IntToBinary(0)+"\n"+
    string(BinaryToInt("0111"))+"==7=="+IntToBinary(7)+"\n"+
    string(BinaryToInt("1000"))+"==8=="+IntToBinary(8)+"\n"+
    string(BinaryToInt("1111"))+"==15=="+IntToBinary(15)+"\n"
  );
  Sleep(500);
}

int BinaryToInt(string binary){  // With thanks, concept from http://www.cplusplus.com/forum/windows/30135/ (though the code on that page is faulty afaics)
  int out=0;
  if(StringLen(binary)==0){return(0);}
  for(int i=0;i<StringLen(binary);i++){
    if(StringSubstr(binary,i,1)=="1"){
      out+=int(MathPow(2,StringLen(binary)-i-1));
    }else{
      if(StringSubstr(binary,i,1)!="0"){
        Assert("Invalid binary string passed to BinaryToInt: "+binary);
      }
    }
  }
  return(out);
}

string IntToBinary(int i){  // With thanks, code from https://forum.mql4.com/65906#1001494
  if(i==0) return "0";
  if(i<0) return "-" + IntToBinary(-i);
  string out="";
  for(;i!=0;i/=2) out=string(i%2)+out;
  return(out);
}

void Assert(string text){
  Comment(string(TimeLocal())+" Assert: "+text);
  Print(string(TimeLocal())+" Assert: "+text);
  ExpertRemove();
  int a=0;double b=1/a;  // DivBy0 for cases where ExpertRemove() fails
}

Code provided as-is. No warranties. I tested it, and for me it works fine. Test included.

 

Another little goodie: you likely want to use binary strings with a certain prefix lenght. For example, "1" (decimal) should render as "0001" and not as "1" - which is what the current functions do.

You could amend this in the function (by adding a "lenght" variable), or post-process it as needed. Here is an example of post-processing:

int WIDTH=4, NR_TO_CONVERT=1;
string s1="",s1prep=IntToBinary(NR_TO_CONVERT); 
StringInit(s1,WIDTH-StringLen(s1prep),'0');
s1=StringConcatenate(s1,s1prep);
Comment(s1);  

The number to convert from decimal to binary is 1. The desired lenght of the binary string is 4. The output is indeed "0001", as required by WIDTH=4, and not just "1".

 
Roel13:

Another little goodie: you likely want to use binary strings with a certain prefix lenght. For example, "1" (decimal) should render as "0001" and not as "1" - which is what the current functions do.

You could amend this in the function (by adding a "lenght" variable), or post-process it as needed. Here is an example of post-processing:

The number to convert from decimal to binary is 1. The desired lenght of the binary string is 4. The output is indeed "0001", as required by WIDTH=4, and not just "1".

If I understood you right, then the aim of this code snipplet is "adding leading zeroes" to a binary result. So the result shows as 0001 rather than 1. This could be achieved easier with the StringFormat function:

string newString = StringFormat("%04d", binResult);
 
BinResult is a string.
string newString = StringFormat("%04s", binResult);
 
@WHRoeder: Your changes result in a different visual output.
 
string newString = StringFormat("%04d", StringToInteger(binResult));  // return long

or

string newString = StringFormat("%04d", StrToInteger(binResult));  // return int