Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
250 views
in Technique[技术] by (71.8m points)

How do I convert a string version of a number in an arbitrary base to an integer?

how to convert string to integer??

for ex:

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

any code will be helpfull

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...