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:
🌐
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.
🌐
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 ►
🌐
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.
🌐
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 ...
🌐
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.
🌐
Reddit
reddit.com › r/algorithms › most efficient way to convert an integer into a binary string? (eg. 4 -> "100")
r/algorithms on Reddit: Most efficient way to convert an integer into a binary string? (eg. 4 -> "100")
July 15, 2015 -

So this was involved in an interview question I was practicing with. The way I see it, there are three different ways to do this: ("N" refers to the number to convert below)

  1. Make a blank string variable which will contain the string. Continually do a bitwise AND with the number and 1 and right shift the number by 1 after each and. This will continually extract the rightmost bit (LSB) in the integer. AFter extracting that bit value, convert it to its string value by doing some ascii math/whatever, and then prepend it on to the string. The issue I see with this is that prepending to a string can be inefficient (This was why I asked about prepending vs appending to a string in one of my earlier posts). The runtime efficiency of this would be O(#of bits in number * time to prepend to string), so I guess something like O(logN * N) ? Assuming prepending is O(N)

  2. Do the same thing as number 1, except instead of prepending to the string, append to the string and then reverse it at the end. This would also be O(NlogN), assuming appending is O(1) and reversing is O(N). However, if appending could potentially be O(N) because the string could be stored in an array which has a finite capacity and can fill up, then that would make it O(N2 logN)? I'm not too sure about my big O analysis here so I was wondering if someone could vouch for the correctness of this.

  3. Figure out the number of bits contained in the number by doing floor(log2(N)) + 1 (we could count each bit individually, but I'm assuming the logarithm is more efficient? I don't know the big O runtime of taking a logarithm though - Lets call this value "numBits"). Once we know that, make a bitmask by doing 1 << numBits. Then, continually do bitwise AND with the number and the mask, and that will extract the LSB. Convert it to the string equivalent with ascii math/whatever the fuck, and then append it to the string. Then, shift the mask 1 to the right. This method doesn't require a O(N) reversal operation or a O(N) prepending operation. If we assume appending to the string is O(1), then this algorithm's runtime would be proportional to the number of bits contained in the number, so it would be O(logN), which makes it the best of the three. I guess if we assume appending is O(N) however, then it would be O(NlogN), wouldn't it? This also depends on the runtime efficiency of taking a logarithm, and I am not sure if that can be considered constant time. Im assuming O(time to take logarithm) < O(logN) though.

Which of these would you recommend I use if I was asked to "write a function to convert an integer to a binary encoded string"?

Top answer
1 of 5
8
Algorithm for C/C++ create a 16-element array of strings: "0000", "0001", "0010", "0011", etc to store the bit representations of a nibble (4-bits). If you want to do a byte at a time, you'll need a larger array of "00000000", "00000001", etc -- but a nibble should be good enough 2. go thru the number a nibble at a time, converting these 4-bits to an 8-bit integer (shift and mask), looking it up, and appending 3. To append strings, start at the MSB and work your way down a nibble at a time To prepend strings, start at the LSB and write it from END of the character buffer working your way towards the beginning of the string. At the end, return the pointer to the first byte of the string (which could be in the middle of your buffer, etc)
2 of 5
2
1. Careful, your running times are a pretty messy. You're mixing different Ns most of the time. For example: The runtime efficiency of this would be O(#of bits in number * time to prepend to string), so I guess something like O(logN * N) ? Assuming prepending is O(N) The second N is not the number itself, but the length of the string. Taken literally, you're describing exponential runtime here, because the input length is log(N). The string length is bounded by the encoding length of your number, so log(N). So the runtime you're looking for is O(log²N). 2. Same problem with the Ns and I don't know where your squared string length comes from. You want O(#bits * string length (appending) + string length(reversal)), so this is O(log N * log N + log N) = O(log²N). Btw, if you use dynamically growing arrays for the appending phase, then the running time of appending is constant , so you'd be down to O(log N). 3. The logarithm is absolutely irrelevant here. Looking for the most significant set bit manually runs in O(log N), so for the sake of runtime analysis that is fine. There is also an x86 instruction to handle that for you, so you could chalk it up as "constant" time, but it doesn't really matter. The running time of your algorithm is O(time for log + time to allocate array + time to fill array) = O(3 log N) = O(log N).