• 0 Vote(s) - 0 Average
• 1
• 2
• 3
• 4
• 5
[Tut] How to Convert Hex String to Integer in Python

How to Convert Hex String to Integer in Python

## Problem Formulation

Given a string in hexadecimal form:

```s = '0xff'
# or: s = 'ff'```

How to convert the hex string to an integer in Python?

For example, you want to convert the hexadecimal string `'0xff'` to the decimal integer `255`.

Here are a few other examples:

```0x0 --> 0
0x4 --> 4
0x8 --> 8
0x12 --> 18
0x16 --> 22
0x20 --> 32
0x24 --> 36
0x28 --> 40```

## Hex String to Integer using int() with base 16

To convert a hexadecimal string to an integer, pass the string as a first argument into Python’s built-in `int()` function. Use `base=16` as a second argument of the `int()` function to specify that the given string is a hex number. The `int()` function will then convert the hex string to an integer with base 10 and return the result.

Here’s a minimal example:

```>>> int('0xff', base=16)
255```

## Examples

And here’s how you can convert the additional examples shown above:

```>>> int('0x0', base=16)
0
>>> int('0x4', base=16)
4
>>> int('0x8', base=16)
8
>>> int('0x12', base=16)
18
>>> int('0x16', base=16)
22
>>> int('0x20', base=16)
32
>>> int('0x24', base=16)
36
>>> int('0x28', base=16)
40```

You actually don’t need to use the prefix `'0x'` because your second argument already defines unambiguously that the given string is a hexadecimal number:

```>>> int('0', base=16)
0
>>> int('4', base=16)
4
>>> int('8', base=16)
8
>>> int('12', base=16)
18
>>> int('16', base=16)
22
>>> int('20', base=16)
32
>>> int('24', base=16)
36
>>> int('28', base=16)
40```

However, skipping the base but leaving the prefix raises a `ValueError: invalid literal for int() with base 10: '0x28'`:

```>>> int('0x28')
Traceback (most recent call last): File "<pyshell#19>", line 1, in <module> int('0x28')
ValueError: invalid literal for int() with base 10: '0x28'```

It assumes that the input string is in base 10 when in fact, it isn’t.

Note: Even though passing a prefixed string `'0x...'` into the `int()` function is unambiguous, Python’s `int()` function doesn’t accept it if you don’t also define the base. This may be fixed in future versions!

In fact, you can specify the base argument as 0 to switch on base guessing—which should be the default behavior anyway!

## Base Guessing

You can pass a prefixed string `'0x...'` into the `int()` function and set the base to 0 to switch on base guessing in Python. This uses the prefix to determine the base automatically—without you needing to set it to `16`. Yet, you still have to set it to `0` so the benefit is marginal in practice.

```>>> int('0x9', base=16)
9
>>> int('0x9', base=0)
9
>>> int('0x9', 0)
9```

## Converting Hex Literals to Int

If you don’t have a hex string but a hex number—called a literal—such as `0xff`, you don’t even need the `int()` function because Python will automatically convert it to a decimal number:

```>>> 0x10
16
>>> 0xff
255```

## Background int()

`Syntax: int(value [, base]) --> int`

Do you still need more background information about Python’s built-in `int()` function? No problem, read over the related tutorial.

Related Tutorial: Python’s Built-in `int()` Function

## Hex to Int Table

Just for fun, here are the hex to int conversions of the powers of two:

The post How to Convert Hex String to Integer in Python first appeared on Finxter.

https://www.sickgaming.net/blog/2021/03/...in-python/

 Possibly Related Threads… Thread Author Replies Views Last Post [Tut] How to Check if a Python String Contains Only Digits? xSicKxBot 0 8 04-12-2021, 12:23 PM Last Post: xSicKxBot [Tut] How to Convert a String List to an Integer List in Python xSicKxBot 0 11 04-09-2021, 01:50 PM Last Post: xSicKxBot [Tut] How to Convert a Float List to a String List in Python xSicKxBot 0 14 04-07-2021, 02:53 PM Last Post: xSicKxBot [Tut] Python String to Float Conversion xSicKxBot 0 20 04-01-2021, 11:07 PM Last Post: xSicKxBot [Tut] Python OpenCV Image Processing – Resize, Blend, Blur, Threshold, Convert xSicKxBot 0 22 03-23-2021, 12:13 PM Last Post: xSicKxBot [Tut] Python String zfill() xSicKxBot 0 19 03-22-2021, 06:30 PM Last Post: xSicKxBot [Tut] How to Create a Tuple from a String and a List of Strings xSicKxBot 0 30 03-13-2021, 09:55 AM Last Post: xSicKxBot [Tut] How to Print an Integer with Commas as Thousands Separators in Python? xSicKxBot 0 48 02-08-2021, 02:31 PM Last Post: xSicKxBot [Tut] How to Remove Everything After the Last Character in a String? xSicKxBot 0 55 02-02-2021, 08:23 AM Last Post: xSicKxBot [Tut] How To Cut A String In Python? xSicKxBot 0 60 02-01-2021, 04:09 PM Last Post: xSicKxBot

Forum Jump:

 Active Threads News - Nintendo Actually Responded To A ... Last Post: xSicKxBot Today 01:50 AM » Replies: 0 » Views: 5 News - Rumour: Dragon Ball Z: Kakarot Co... Last Post: xSicKxBot Today 01:50 AM » Replies: 0 » Views: 5 Xbox Wire - Next Week on Xbox: April 6 t... Last Post: xSicKxBot Yesterday 11:48 PM » Replies: 0 » Views: 7 News - Ubisoft hires ex-Uber HR director... Last Post: xSicKxBot Yesterday 11:48 PM » Replies: 0 » Views: 12 News - Blog: An essential guide to live ... Last Post: xSicKxBot Yesterday 11:48 PM » Replies: 0 » Views: 10 [Tut] Python Dash Book Last Post: xSicKxBot Yesterday 05:52 PM » Replies: 0 » Views: 6 (Indie Deal) Adventure Tales Bundle, Qua... Last Post: xSicKxBot Yesterday 05:52 PM » Replies: 0 » Views: 7 News - Good Job! Is The Next Nintendo Sw... Last Post: xSicKxBot Yesterday 05:49 PM » Replies: 0 » Views: 8 News - Random: Halo Instagram Shows Off ... Last Post: xSicKxBot Yesterday 05:49 PM » Replies: 0 » Views: 7 News - Guide To Refresh Rates And Respon... Last Post: xSicKxBot Yesterday 05:49 PM » Replies: 0 » Views: 16