StringFormat hex output parameters... how to convert from hex to uchar or string

 

Hi... i am looking at documentation of CryptEncode function and there are a good example code... there are a function ArrayToEx that converts a uchar array to a Hexadecimal string... using StringFormat and "%.2X" as the output parameter.

Well, i need to know how to do the opposite: Fill an uchar array using an hexadecimal string... Here an example of converting my name to hex:


//+------------------------------------------------------------------+
//|                                                        ToHex.mq4 |
//|                                         Wemerson Couto Guimaraes |
//+------------------------------------------------------------------+
#property copyright "Wemerson Couto Guimaraes"
#property link      ""
#property version   "1.00"
#property strict

// Simple script to convert a value to be represented in hexadecimal

//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
      string myname="Wemerson";
      uchar arr[];
      StringToCharArray(myname, arr);
      
      string hex=ArrayToHex(arr);
      
      printf("My Name: %s in Hex is: %s", myname, hex);
   
  }
//+------------------------------------------------------------------+

string ArrayToHex(uchar &arr[],int count=-1)
  {
   string res="";
//--- check
   if(count<0 || count>ArraySize(arr))
      count=ArraySize(arr);
//--- transform to HEX string
   for(int i=0; i<count; i++)
      res+=StringFormat("%.2X",arr[i]);
//---
   return(res);
  }


The result is:

2017.10.11 10:40:47.411 ToHex EURUSD,H1: My Name: Wemerson in Hex is: 57656D6572736F6E00

Now i waht to convert the hex value 57656D6572736F6E00 to the string represented by this hex string...

CryptEncode - Common Functions - MQL4 Reference
CryptEncode - Common Functions - MQL4 Reference
  • docs.mql4.com
//| ArrayToHex                                                       | //| Script program start function                                    |
 
Wemerson Guimaraes: Now i waht to convert the hex value 57656D6572736F6E00 to the string represented

So what's stopping you?

Not compiled, not tested.

string long_to_hex(ulong value){
   string result="";
   do{
      result = string(value & 0xF)+result;
      value >>= 8;
   }while(value != 0);
   return result;
}

Not compiled, not tested.

Was that so hard?
 
whroeder1:

So what's stopping you?

Thank you for your support... Well... i found how to convert an string to an string containing the HEX chars... as i show in my example... but What is istopping me is is that i don't know how to do the inverse... convert a string containing HEX chars back to the original string.... 

whroeder1:

Was that so hard?

Yep for me it's hard, this is the reason i came here looking for help! If my knowlegment was so extense like yours i believe i will not need to beg for help here... 


About your suggestion, it is for converting a numeric value to hexadecimal right ? Well, converting to HEX i know how to do using uchar array and StringFormat output parameter "%.2X"... but i need to to the opposite...  

 
Wemerson Guimaraes: it is for converting a numeric value to hexadecimal right ? Well, converting to HEX i know how to do using uchar array and StringFormat output parameter "%.2X"... but i need to to the opposite...  
  1. Converting a numeric to a hex string, I gave you.
  2. A uchar array and a string are the same thing, different formats.
  3. You need "the opposite" of what? Show us your attempt and state the nature of your problem.
              No free help
              urgent help.
 
whroeder1:
  1. Converting a numeric to a hex string, I gave you.
  2. A uchar array and a string are the same thing, different formats.
  3. You need "the opposite" of what? Show us your attempt and state the nature of your problem.
              No free help
              urgent help.

I found exactly what i need here: Joao Rosas hexToInteger function

Thank you for your appointments. Your the best my friend!

 
static char __hex [] = { '0', '1', '2', '3', '4', '5', '6', '7',
                        '8', '9' ,'A', 'B', 'C', 'D', 'E', 'F' };
string IntegerToHexString(uint num)
{
   int len=0,k=0;
   char buff[64];
   do
      {
      uint n=num&0xF;
      buff[len] = __hex[n];
      len++;
      num>>=4;
      }
   while(num!=0);
   
   for(;k<len/2;k++)
      {
      buff[k]^=buff[len-k-1];
      buff[len-k-1]^=buff[k];
      buff[k]^=buff[len-k-1];
      }
   return CharArrayToString(buff,0,len);
}

Unfortunately the static var __hex cannot be inside the function as a local static variable, because it will not be intialized correctly. MQ supports this is normal, but it´s not, it´s a bug of the compiler. 

Reason: