[Python memo] I want to get a 2-digit hexadecimal number from a decimal number

Given a decimal number, consider how to get a hexadecimal number. As an example, using the int "227", I want the hexadecimal number "e3" below.

Use hex function

If you think about it normally, it's like this

hex(227)
'0xe3'

I don't want 0x. You can replace it as a string, but ...

hex(227).replace('0x','')
'e3'

It ’s not very clear.

Use string format

There is also such a thing

'{:x}'.format(227)
'e3'

Somewhat better than replace. .. ..

But isn't it something that doesn't go smarter?

reference

[Addition] Formatted string literal

I told you in the comments. If it is 3.6 or later, it seems that you can write it like this using ** formatted string literal **.

f'{227:02x}'

http://docs.python.jp/3.6/reference/lexical_analysis.html#f-strings

It feels like a more abstract string format with%. Unfortunately, many DCC tools are not yet compatible with Series 3. .. .. Wait 2019 (was it?

Reverse

int('e3',16)
227

int('0xe3',16)
227

It takes a lot of work to get e3 from 227, but you can get 227 from e3 in one shot, right?

document

Recommended Posts

[Python memo] I want to get a 2-digit hexadecimal number from a decimal number
[Python] I want to get a common set between numpy
I want to start a lot of processes from python
I want to send a message from Python to LINE Bot
I want to use jar from python
I want to build a Python environment
[Python] A memo that I tried to get started with asyncio
I tried to create a program to convert hexadecimal numbers to decimal numbers with python
I want to create a window in Python
I want to email from Gmail using Python.
[Python] I want to manage 7DaysToDie from Discord! 1/3
I want to make a game with Python
I tried to discriminate a 6-digit number with a number discrimination application made with python
I want to use ceres solver from python
[Python] I want to manage 7DaysToDie from Discord! 2/3
I want to make C ++ code from Python code!
I want to write to a file with Python
I want to get / execute variables / functions / classes of external files from Python
[Python] I tried to get the type name as a string from the type function
I want to get the file name, line number, and function name in Python 3.4
I want to embed a variable in a Python string
I want to easily implement a timeout in python
I want to iterate a Python generator many times
I want to generate a UUID quickly (memorandum) ~ Python ~
I want to write in Python! (2) Let's write a test
I want to randomly sample a file in Python
I want to work with a robot in python.
[Python3] I want to generate harassment names from Japanese!
[Python] I want to make a nested list a tuple
I want to run a quantum computer with Python
I want to use a python data source in Re: Dash to get query results
[Python] I made a system to introduce "recipes I really want" from the recipe site!
I want to tell people who want to import from a higher directory with Python direnv
I tried "How to get a method decorated in Python"
I want to install a package from requirements.txt with poetry
I want to make input () a nice complement in python
I want to debug with Python
I want to use a wildcard that I want to shell with Python remove
How to get a string from a command line argument in python
[Python] How to get & change rows / columns / values from a table.
I want to do a full text search with elasticsearch + python
[Introduction] I want to make a Mastodon Bot with Python! 【Beginners】
I want to make a parameter list from CloudFormation code (yaml)
I want to get information from fstab at the ssh connection destination and execute a command
I want to print in a comprehension
Send a message from Python to Slack
How to get a stacktrace in python
I want to analyze logs with Python
I want to play with aws with python
I want to send a signal only from the sub thread to the main thread
A story I was addicted to when inserting from Python to a PostgreSQL table
Mayungo's Python Learning Episode 6: I tried to convert a character string to a number
How to get a value from a parameter store in lambda (using python)
I want to convert a table converted to PDF in Python back to CSV
I get a UnicodeDecodeError when trying to connect to oracle with python sqlalchemy
I want to color a part of an Excel string in Python
Python: I want to measure the processing time of a function neatly
I want to make a web application using React and Python flask
I want to do a monkey patch only partially safely in Python
I want to pass an argument to a python function and execute it from PHP on a web server
I want to make matplotlib a dark theme