
Binary numbers are the foundation of everything in computing - representing on/off states (1s and 0s) at the lowest level of data.
But to work effectively with binary, you'll often need to convert it into other formats like hexadecimal (hex), octal, decimal, or even text strings.
In this guide, you'll learn how to perform these conversions in Python and other popular programming languages such as JavaScript, C#, Java, and C++.
If you want to experiment instantly, try DevGizmo's Binary Convertor. A free, instant tool that converts between binary, decimal, octal, and hex with one click.
Before diving into code, let's recap the basics:
| Number Systems | Base | Digits Used | Example |
|----------------|------|-------------|-----------------------|
| Binary | 2 | 0, 1 | 1010 = 10 in decimal |
| Octal | 8 | 0-7 | 12 = 10 in decimal |
| Decimal | 10 | 0-9 | 10 = 10 in decimal |
| Hexadecimal | 16 | 0-9, A-F | A = 10 in decimal |Each system represents the same value differently - conversions simply reinterpret those representations.
Python has native support for all major base conversions.
Binary to Decimal
binary = "1010"
decimal = int(binary, 2)
print(decimal) # Output: 10
Binary to Hexadecimal
binary = "1010"
hexadecimal = hex(int(binary, 2))
print(hexadecimal) # Output: 0xa
Binary to Octal
binary = "1010"
octal = oct(int(binary, 2))
print(octal) # Output: 0o12
Binary to String (Text)
binary = "0100100001100101011011000110110001101111"
string = ''.join(chr(int(binary[i:i+8], 2)) for i in range(0, len(binary), 8))
print(string) # Output: Hello
Tip:
Use built-in bin(), hex(), and oct() functions for direct conversions between numbers.
JavaScript handles number bases using the parseInt() and toString() methods.
Binary to Decimal
let binary = "1010";
let decimal = parseInt(binary, 2);
console.log(decimal); // 10
Binary to Hexadecimal
let hex = parseInt(binary, 2).toString(16);
console.log(hex); // a
Binary to Octal
let octal = parseInt(binary, 2).toString(8);
console.log(octal); // 12
Binary to String (Text)
function binaryToString(bin) {
return bin.match(/.{1,8}/g)
.map(byte => String.fromCharCode(parseInt(byte, 2)))
.join('');
}
console.log(binaryToString('0100100001100101011011000110110001101111')); // Hello
C# can perform base conversions using Convert.ToInt32() and Convert.ToString() methods.
Binary to Decimal
string binary = "1010";
int decimalValue = Convert.ToInt32(binary, 2);
Console.WriteLine(decimalValue); // 10
Binary to Hex
string hex = Convert.ToInt32(binary, 2).ToString("X");
Console.WriteLine(hex); // A
Binary to Octal
string octal = Convert.ToString(Convert.ToInt32(binary, 2), 8);
Console.WriteLine(octal); // 12
Binary to String (Text)
string binaryText = "0100100001100101011011000110110001101111";
string result = "";
for (int i = 0; i < binaryText.Length; i += 8)
{
string byteString = binaryText.Substring(i, 8);
result += (char)Convert.ToInt32(byteString, 2);
}
Console.WriteLine(result); // Hello
Java uses Integer.parseInt() and Integer.toString() for conversions.
Binary to Decimal
String binary = "1010";
int decimalValue = Integer.parseInt(binary, 2);
System.out.println(decimalValue); // 10
Binary to Hexadecimal
String hex = Integer.toHexString(Integer.parseInt(binary, 2));
System.out.println(hex); // a
Binary to Octal
String octal = Integer.toOctalString(Integer.parseInt(binary, 2));
System.out.println(octal); // 12
Binary to String (Text)
String binaryText = "0100100001100101011011000110110001101111";
StringBuilder sb = new StringBuilder();
for (int i = 0; i < binaryText.length(); i += 8) {
String byteString = binaryText.substring(i, i + 8);
sb.append((char) Integer.parseInt(byteString, 2));
}
System.out.println(sb.toString()); // Hello
In C++, conversions require a mix of bitset and standard string manipulation.
Binary to Decimal
#include <iostream>
#include <string>
int main() {
std::string binary = "1010";
unsigned long decimal = std::stoul(binary, nullptr, 2);
std::cout << "Decimal: " << decimal << std::endl;
return 0;
}
Binary to Hexadecimal
#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>
int main() {
std::string binary = "1010";
unsigned long decimal = std::stoul(binary, nullptr, 2);
std::stringstream ss;
ss << std::hex << std::uppercase << decimal;
std::cout << "Hexadecimal: " << ss.str() << std::endl;
return 0;
}
Binary to Octal
#include <iostream>
#include <string>
#include <sstream>
int main() {
std::string binary = "1010";
unsigned long decimal = std::stoul(binary, nullptr, 2);
std::stringstream ss;
ss << std::oct << decimal;
std::cout << "Octal: " << ss.str() << std::endl;
return 0;
}
Binary to String (Text)
#include <iostream>
#include <bitset>
using namespace std;
int main() {
string binary = "0100100001100101011011000110110001101111";
string text = "";
for (size_t i = 0; i < binary.size(); i += 8) {
bitset<8> bits(binary.substr(i, 8));
text += char(bits.to_ulong());
}
cout << text; // Hello
}
| Conversion | Common Use Case |
|-----------------------|------------------------------------------|
| Binary -> Decimal | Interpreting numeric values |
| Binary -> Hex | Debigging memory or encoding data |
| Binary -> Octal | File permissions, legacy systems |
| Binary -> String | Readings binary-encoded text or files |You can convert between Binary, Hex, Octal, Decimal, and even Text Strings instantly using DevGizmos's Binary Converter
No Installation needed - just type or paste your binary and see all formats in real time.
Binary conversion is a core concept for any developer working with data, encoding, or hardware-level logic. Whether you're building encoders, network tools, or digital systems, knowing how to switch between binary, hex, octal, and decimal is essential.
You've now seen how to perform all these conversion in Python, JavaScript, C#, Java, and C++ and can experiment instantly with DevGizmo's Binary Converter.
Learn how to convert binary values into hex, octal, decimal, and string formats in Python, JavaScript, C#, Java, and C++. This complete guide covers every major conversion method with examples and explains when to use each. Try binary conversions instantly with DevGizmo's Binary Converter.