Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin: How to convert from binary to decimal

Tags:

kotlin

I need to convert from Binary String to Int or Long in Kotlin. Is there any inbuilt utility method available for the same?

like image 693
Anurag Shukla Avatar asked Dec 23 '22 15:12

Anurag Shukla


1 Answers

that's how you can do;

println("11001".toInt(2))
println("11001".toLong(2))

As Joffrey said in the comment, "the 2 here is the number's base. That's why it's 2 for binary, would be 16 for hexadecimal"

like image 163
erolkaya84 Avatar answered Jan 13 '23 16:01

erolkaya84