Search code examples
c#converterslong-integerstrtol

Finding a C# equivalent (that specifies a base parameter) to strtol


I need to convert a string to a long in c#. I'm porting a C++ program that currently uses strtol to do this. Because MSDN defines a long data type as a "signed 64 bit integer", I am using the following line of code to do the conversion in C#:

long value = Convert.ToInt64(stringVal);    

My question, however, is how do I specify the base value that strtol utilizes, with System.Convert... (Do I even need it?)? I know there are other question about the C# equivalent for this C++ utility, but I didn't find any that asked about equating the parameters.

The definition of strtol is: long int strtol (const char* str, char** endptr, int base);


Solution

  • You just need to add the base parameter to your call.

    long value = Convert.ToInt64(stringVal, base);
    

    where base is the base of the number.