tags:

views:

95

answers:

3

how to convert string to integer??

for ex:

  • "5328764",to int base 10
  • "AB3F3A", to int base 16

any code will be helpfull

+7  A: 

Assuming arbitrary base (not 16, 10, 8, 2):

In C (C++), use strtol

return strtol("AB3F3A", NULL, 16);

In Javascript, use parseInt.

return parseInt("AB3F3A", 16);

In Python, use int(string, base).

return int("AB3F3A", 16)

In Java, use Integer.parseInt (thanks Michael.)

return Integer.parseInt("AB3F3A", 16);

In PHP, use base_convert.

return intval(base_convert('AB3F3A', 16, 10));

In Ruby, use to_i

"AB3F3A".to_i(16)

In C#, write one yourself.

KennyTM
Integer.parseInt
Michael Mrozek
Why would this answer get a downvote? What is wrong with it?
Sinan Y.
Why the downvote? If this is a legitimate novice question then KennyTM really covered the bases. If this is a homework question then using a standard library function is probably not allowed so we're not doing the work for the OP.
ZoogieZork
A: 

9999 is really 9000 + 9000 + 90 + 9 So, start at the right hand side of the string, and pick off the numbers one at a time. Each character number has an ASCII code, which can be translated to the number, and multiplied by the appropriate amount.

Jeremy
A: 

in C#, i think it is: Convert.ToInt64(value, base) and the base must be 2, 8, 10, or 16