Skip to content
Home » Converting String Representations of Binary, Octal and Hexadecimal Integers

Converting String Representations of Binary, Octal and Hexadecimal Integers

Spread the love

In my previous article we were talking about converting string representations of numbers to numbers. Today we’ll be talking about converting string representations of binary, octal and hexadecimal integers.

Before you start, you may want to read my older articles on binary, octal and hexadecimal numbers:

Binary Numbers in Python

Decimal Number vs Binary, Octal and Hexadecimal

And now let’s see how to convert a string representation of a binary, octal or hexadecimal integer to an appropriate integer with the specified base.

Here’s the video version of this article:

We can convert a string representation of a binary, octal or hexadecimal integer to an appropriate integer with the specified base using the following syntax:

int(number_string_representation, base)

Here are some examples:

– a binary number

 >>> binary_number = "1001"
 >>> int(binary_number, 2)
 9 

– an octal number

 >>> octal_number = "10"
 >>> int(octal_number, 8)
 8 

– a hexadecimal number

 >>> hexadecimal_number = "F1"
 >>> int(hexadecimal_number, 16)
 241 

Your Panda3D Magazine

Make Awesome Games and Other 3D Apps

with Panda3D and Blender using Python.

Cool stuff, easy to follow articles.

Get the magazine here (PDF).

Python Jumpstart Course

Learn the basics of Python, including OOP.

with lots of exercises, easy to follow

The course is available on Udemy.

Blender Jumpstart Course

Learn the basics of 3D modeling in Blender.

step-by-step, easy to follow, visually rich

The course is available on Udemy and on Skillshare.


Spread the love

Leave a Reply