How to convert string to ulong?

 

There is StringToInteget function having long as return type. What about ulong?

Tickets have ulong type. Assume I need to store a ticket as the string then load it and convert back to ulong.

How to do? MQL5 has not StringToUlong function.

 

Store a ticket in ulong. No need for string.

ulong ticket = m_position.Ticket();
 

Like this?

void OnStart()
{
   string strValue = "18446744073709551615";
   
   ulong ulongValue = (ulong)strValue;
   
   Alert("ulongValue = ", ulongValue);
}
 
Sergey Eremin:

Like this?

It works. Thank you!
 
RickD:

There is StringToInteget function having long as return type. What about ulong?

Tickets have ulong type. Assume I need to store a ticket as the string then load it and convert back to ulong.

How to do? MQL5 has not StringToUlong function.

StringToInteger() works with ulong.

 
Alain Verleyen:

StringToInteger() works with ulong.

But IntegerToString does not work with ulong.

void OnStart()
{
  ulong ul = 18446744073709551615;
  PrintFormat("ul= %I64u", ul);
  
  string str = IntegerToString(ul);
  PrintFormat("str= %s", str);
}
2019.11.15 00:03:59.078 s-Test (EURUSD,H1)      ul= 18446744073709551615
2019.11.15 00:03:59.083 s-Test (EURUSD,H1)      str= -1
 

By the way , if you want to store trade data in files you can assign a structure array 

struct trades
{
ulong ticket;
datetime opentime;
double sl,tp,op,cp;
//etc
};
trades TRADE_LOG[];

*edit : and ticket will be stored as :

TRADE_LOG[i].ticket=...; as Enrique said

And when it comes time to Save you can 

FileWriteArray(handle,TRADE_LOG,0,WHOLE_ARRAY);

Which will store the entire array on the file ,and to Load it you simply 

FileReadArray(handle,TRADE_LOG);
no need for strings is all im saying
 
RickD:

But IntegerToString does not work with ulong.

Of course as this specific value you used is -1 as long. For a ticket it would not be a problem as such big values are never used.

Anyway, no need fro IntegerToString(), which is only useful if you need to format the string (2nd and 3rd parameters).

Simply use :

  string str = (string)ul;

But the main question remains, why do you want to store it as a string ?

 
Alain Verleyen #:

Of course as this specific value you used is -1 as long. For a ticket it would not be a problem as such big values are never used.

Anyway, no need fro IntegerToString(), which is only useful if you need to format the string (2nd and 3rd parameters).

Simply use :

But the main question remains, why do you want to store it as a string ?

Inorder to store it in a MySQL database.

 

This code defines two functions, StringToUlong for converting a string to an unsigned long integer, and UlongToString for converting an unsigned long integer to a string. You can use these functions to store ticket numbers as strings and convert them back to ulong when needed.

 ulong StringToUlong(string str)
{
    int intValue = StringToInteger(str); // Convert string to integer
    ulong ulongValue = (ulong)intValue;  // Cast integer to ulong
    return ulongValue;
}

// Convert ulong to string
string UlongToString(ulong value)
{
    string strValue = IntegerToString(value); // Convert ulong to string
    return strValue;
}

// Example usage
void Example()
{
    // Convert ulong to string and store it
    ulong ticketNumber = 1234567890;
    string ticketString = UlongToString(ticketNumber);

    // Load the string and convert it back to ulong
    ulong loadedTicketNumber = StringToUlong(ticketString);
} 
 
Seleucus #:

Inorder to store it in a MySQL database.

MySQL has the bytes twisted, while in MQL the byte order is from left to right, when sending or receiving the data from MySQL, it is right to left.

So you need to flip the bytes of your ulong.

Additionally, it is important to know, SQlite cannot handle ulong values, you can only store long values. If you define your MySQL table, make sure to use BIGINT unsigned so that your value doesn't get converted on the server side.