0

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);

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Eric after dark
  • 1,768
  • 4
  • 31
  • 79

2 Answers2

2

You're close:

Convert.ToInt64("abc", 16)
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
2

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.

Steve Mitcham
  • 5,268
  • 1
  • 28
  • 56