Author: Itso
DecBinHex

This script serves as a specialized utility library designed to perform "data translation" between different numerical formats. In computing, there are various ways to represent numbers; this tool allows for seamless conversion between the standard numbers we use daily (Decimal), the binary code used by computers (Binary), and the shorthand codes often used in high-level programming and memory addressing (Hexadecimal).

The logic of the script is divided into four distinct functional components:

1. Decimal to Binary Conversion This function takes a standard whole number and breaks it down into its simplest form—the "on" and "off" bits (0s and 1s) that represent the value in computer memory. It works by repeatedly dividing the number to isolate each individual bit until the entire sequence is constructed.

2. Binary to Decimal Conversion This acts as the reverse of the first function. It takes a string of 0s and 1s and translates it back into a human-readable whole number. It does this by scanning the binary string from left to right, calculating the weight of each position to rebuild the original value.

3. Decimal to Hexadecimal Conversion Hexadecimal is a base-16 system commonly used in technical environments because it can represent large values more compactly than binary. This function translates a standard number into this "base-16" format. It identifies the remainder of the number when divided by 16 and converts it into either a digit (0–9) or a letter (A–F) to create the final shorthand code.

4. Hexadecimal to Decimal Conversion The final component translates the shorthand hexadecimal codes back into standard decimal numbers. The logic is sophisticated enough to recognize all variations of hex characters, including standard numbers, uppercase letters, and lowercase letters. It identifies each character in the string, assigns its correct numerical value, and combines them to provide the accurate total.

Summary for Users Essentially, this script provides the mathematical "bridge" required to switch between different languages of numbers. Whether you are trying to see how a value looks in raw computer code or need to convert technical hexadecimal shorthand back into readable numbers, these tools automate those complex calculations instantly.

10 Views
0 Downloads
0 Favorites
DecBinHex
//+------------------------------------------------------------------+
//|                                                    DecBinHex.mq4 |
//|                                                             Itso |
//|                                                      itso@dir.bg |
//+------------------------------------------------------------------+
#property copyright "Itso"
#property link      "itso@dir.bg"
#property library
//+------------------------------------------------------------------+
//| My functions                                                     |
//+------------------------------------------------------------------+
string DecToBin(int n)
  {
   string s = "";
   while(n != 0)
     {
       s = CharToStr(n % 2 + '0') + s;
       n = n / 2;
     }
   return(s);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int BinToDec(string s)
  {
   int p = 0, i;
   for(i = 0; i < StringLen(s); i++)
     {
      p = p*2 + StringGetChar(s,i) - '0';
     }
   return(p);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
string DecToHex(int n)
  {
   string s = "", c;
   while(n != 0)
     {
      if(n%16 < 10)
          c = CharToStr(n % 16 + '0');
      else 
          c = CharToStr(n % 16 + 'A'-10);
      s = c + s;
      n = n / 16;
     }
   return(s);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int HexToDec(string s)
  {
   int p = 0, c, i;
   for(i = 0; i < StringLen(s); i++)
     {
       if((StringGetChar(s, i) >= '0') && (StringGetChar(s, i) <= '9'))
           c = StringGetChar(s, i) - '0';
       else 
           if((StringGetChar(s, i) >= 'A') && (StringGetChar(s, i) <= 'F'))
               c = StringGetChar(s, i) - 'A' + 10;
           else 
               if((StringGetChar(s, i) >= 'a') && (StringGetChar(s, i) <= 'f'))
                  c = StringGetChar(s, i) - 'a' + 10;
               else
                   return(-1);
       p = p*16 + c;
     }
   return(p);
  }
//+------------------------------------------------------------------+

Comments