You need to specify the radix. There's an overload of Integer#parseInt() which allows you to.

int foo = Integer.parseInt("1001", 2);
Answer from Matt Ball on Stack Overflow
🌐
FavTutor
favtutor.com › blogs › binary-string-to-int-python
Convert Binary String to Int in Python (with code)
September 12, 2023 - Learn how to convert binary string to integer in Python programming by using bitstring module and int function.
🌐
RapidTables
rapidtables.com › convert › number › binary-to-string.html
Binary to String Converter
Convert "01010000 01101100 01100001 01101110 01110100 00100000 01110100 01110010 01100101 01100101 01110011" binary ASCII code to text:
🌐
RapidTables
rapidtables.com › convert › number › string-to-binary.html
String to Binary Converter
Enter ASCII/Unicode text string and press the Convert button (e.g enter "Example" to get "01000101 01111000 01100001 01101101 01110000 01101100 01100101"): · Binary to string converter ►
🌐
Teleport
goteleport.com › resources › tools › binary-to-decimal-converter
Binary to Decimal Converter | Instantly Convert Binary to Decimal | Teleport
Python's int() function can be used to effortlessly convert a binary string to a decimal number by specifying the base as 2.
🌐
GeeksforGeeks
geeksforgeeks.org › c# › c-program-to-convert-a-binary-string-to-an-integer
C# Program to Convert a Binary String to an Integer - GeeksforGeeks
July 2, 2020 - // C# program to convert array // of binary string to an integer using System; using System.Text; class GFG { static void Main(string[] args) { // binary number as string string bin_strng = "1010101010101010"; int number = 0; // converting to integer number = Convert.ToInt32(bin_strng, 2); // to print the value Console.WriteLine("Number value of binary \"{0}\" is = {1}", bin_strng, number); } }
🌐
Xojo Programming Forum
forum.xojo.com › general
Convert binary string to integer - General - Xojo Programming Forum
December 17, 2022 - I have a 4 byte string that was read from a file… its binary value is “00 00 00 0C”… How can I convert that into the integer 12? Thanks, Jim
🌐
Reddit
reddit.com › r/learnprogramming › how to convert a binary number to int in assembly mips?
r/learnprogramming on Reddit: How to convert a binary number to int in assembly mips?
March 27, 2024 -

Hi, Im trying to code a program that switches bewteen int, string, binary , etc.. I know how to convert string to int and viceversa but how can you convert a binary number like "110" to binary?? Also, is it recommendable to convert it directly from string to int decimal or first convert the string to int binary and then this int to int decimal??

Find elsewhere
Top answer
1 of 2
56

atoi doesn't handle binary numbers, it just interprets them as big decimal numbers. Your problem is that it's too high and you get an integer overflow due to it being interpreted as decimal number.

The solution would be to use stoi, stol or stoll that got added to string in C++11. Call them like

int i = std::stoi("01000101", nullptr, 2);
  • The returned value is the converted int value.
  • The first argument is the std::string you want to convert.
  • The second is a size_t * where it'll save the index of the first non digit character.
  • The third is an int that corresponds to the base that'll be used for conversion..

For information on the functions look at its cppreference page.


Note that there are also pre C++11 functions with nearly the same name, as example: strtol compared to the C++11 stol.
They do work for different bases too, but they don't do the error handling in the same way (they especially lack when no conversion could be done on the given string at all e.g trying to convert "hello" to a string) and you should probably prefer the C++11 versions.

To make my point, passing "Hello" to both strtol and the C++11 stol would lead to:

  • strtol returns 0 and doesn't give you any way to identify it as error,
  • stol from C++11 throws std::invalid_argument and indicates that something is wrong.

Falsely interpreting something like "Hello" as integers might lead to bugs and should be avoided in my opinion.

But for completeness sake a link to its cppreference page too.

2 of 2
6

It sounds like you should be using strtol() with 2 as the last argument.

🌐
Arduino Forum
forum.arduino.cc › projects › programming
Convert a binary String to an Integer - Programming - Arduino Forum
April 2, 2020 - Hello everyone, I am trying to convert a binary String to an Integer and I wrote the following Code: void setup() { Serial.begin(9600); Serial.print(binToDec("11111")); } void loop() { } int binToDec(String bin) { int l = bin.length(); int dec = 0; for (int i = 0; i
🌐
Medium
dpericich.medium.com › how-to-convert-a-binary-string-to-an-integer-in-ruby-73bed338cd76
How to Convert a Binary String to an Integer in Ruby | by Daniel Pericich | Medium
June 22, 2023 - How to write a Ruby method for converting binary string into an integer value. This solution was created in response to Advent of Code challenge.
🌐
Reddit
reddit.com › r/arduino › convert binary string to integer
Convert Binary String to Integer : r/arduino
October 13, 2023 - We're essentially just "rewriting" the string into an integer but with binary logic instead of integer addition? So, for "1001" we get.. 0000 at declaration · first iteration is 00001 · then 000010 · then 0000100 · then 00001001? edit: praise C++ online compilers ·
🌐
AskPython
askpython.com › home › converting base-2 binary number strings to integers in python
Converting Base-2 Binary Number Strings to Integers in Python - AskPython
March 26, 2023 - Throughout this tutorial, we’ve explored the process of converting binary numbers to decimal integers in Python. With Python’s extensive library of built-in functions and modules, there’s no need for manual calculations. Explicit typecasting is a powerful feature that simplifies this conversion process. We have demonstrated two methods for converting base-2 number strings to integers: using the int() function and the bitstring module.
🌐
IDERA
idera.com › home › converting binary string to integer
Converting Binary String to Integer | IDERA
April 21, 2025 - Convert a binary text string into integer Here is how you convert a binary text string into the corresponding integer value: $binary = "110110110" $int...
🌐
Vultr Docs
docs.vultr.com › java › examples › convert-binary-number-to-decimal-and-vice-versa
Java Program to Convert Binary Number to Decimal and vice-versa | Vultr Docs
September 27, 2024 - This program converts the binary string "1101" to its decimal equivalent. The Integer.parseInt() function interprets the string as a base-2 number and converts it to a base-10 integer.
🌐
Code Beautify
codebeautify.org › string-binary-converter
String to Binary Converter Online tool
Upside Down Text Letter Randomizer ... Builder Intelligent Message Filter Word Replacer Reverse String Text Reverser HTML Encode HTML Decode Base32 Encode Base32 Decode Base58 Encode Base58 Decode Base64 Encode Base64 Decode URL Encode URL Decode String to Hex Converter Hex to String Converter String to Binary Converter ...