Python での 16 進数出力のフォーマット方法

PythonPythonBeginner
オンラインで実践に進む

💡 このチュートリアルは英語版からAIによって翻訳されています。原文を確認するには、 ここをクリックしてください

はじめに

Python は、16 進数を含むさまざまな数値表現を扱うことができる多用途なプログラミング言語です。16 進数は、メモリのアドレス、カラーコード、バイナリデータをよりコンパクトな形式で表現するなど、コンピュータサイエンスのタスクで広く使用されています。このチュートリアルでは、Python で 16 進数出力をフォーマットする方法、その基本的な概念を理解し、いくつかの実用的なアプリケーションを探求します。

16 進数の基本を理解する

16 進数(しばしば「hex」と略されます)は、基数が 16 の数値システムです。10 個の数字(0~9)を使用する日常的な 10 進数システムとは異なり、16 進数は 16 個の異なる記号を使用します。数字の 0~9 と文字の A~F です。

各 16 進数の数字が 10 進数で表すものは次のとおりです。

  • 0~9 は 10 進数と同じです
  • A = 10
  • B = 11
  • C = 12
  • D = 13
  • E = 14
  • F = 15

2 進数から 16 進数への変換

16 進数の主な利点の 1 つは、2 進数との便利な関係です。各 16 進数の数字は、正確に 4 つの 2 進数の数字(ビット)を表します。

Binary: 0000 = Hex: 0
Binary: 0001 = Hex: 1
...
Binary: 1010 = Hex: A
...
Binary: 1111 = Hex: F

16 進数をよりよく理解するために、簡単な Python スクリプトを作成しましょう。

  1. LabEx 環境で WebIDE (VSCode) インターフェースを開きます。
  2. /home/labex/project ディレクトリに hex_basics.py という名前の新しいファイルを作成します。
  3. ファイルに次のコードを追加します。
## Understanding decimal, binary, and hexadecimal conversion
decimal_value = 42

## Convert decimal to binary
binary_value = bin(decimal_value)

## Convert decimal to hexadecimal
hex_value = hex(decimal_value)

## Print all representations
print(f"Decimal: {decimal_value}")
print(f"Binary: {binary_value}")
print(f"Hexadecimal: {hex_value}")

## Let's see a larger number
large_decimal = 255
print(f"\nDecimal: {large_decimal}")
print(f"Binary: {bin(large_decimal)}")
print(f"Hexadecimal: {hex(large_decimal)}")
  1. ファイルを保存します。
  2. スクリプトを実行するには、WebIDE でターミナルを開き、次を実行します。
python3 hex_basics.py

次のような出力が表示されるはずです。

Decimal: 42
Binary: 0b101010
Hexadecimal: 0x2a

Decimal: 255
Binary: 0b11111111
Hexadecimal: 0xff

Python がこれらの数値に自動的にプレフィックスを追加することに注意してください。

  • 2 進数の値には 0b がプレフィックスとして付加されます
  • 16 進数の値には 0x がプレフィックスとして付加されます

これらのプレフィックスは、コード内の異なる数値システムを区別するのに役立ちます。

Python での基本的な 16 進数フォーマット

16 進数の基本を理解したところで、Python で 16 進数の値をフォーマットする方法を学びましょう。Python には、10 進数と 16 進数を変換し、ニーズに合わせて出力をフォーマットするためのいくつかの方法が用意されています。

10 進数と 16 進数の間の変換

16 進数の変換とフォーマットを探索するために、新しいファイルを作成しましょう。

  1. WebIDE で、/home/labex/project ディレクトリに hex_formatting.py という名前の新しいファイルを作成します。
  2. ファイルに次のコードを追加します。
## Basic hexadecimal conversion examples

## Decimal to hex conversion
decimal_number = 171
hex_from_decimal = hex(decimal_number)
print(f"Decimal {decimal_number} converted to hex: {hex_from_decimal}")

## Hex to decimal conversion
hex_number = "0xAB"
decimal_from_hex = int(hex_number, 16)  ## The 16 specifies base-16 (hexadecimal)
print(f"Hex {hex_number} converted to decimal: {decimal_from_hex}")

## You can also use a hex literal directly in your code
hex_literal = 0xAB  ## This is already an integer in Python
print(f"Hex literal 0xAB as decimal: {hex_literal}")

## Working with binary and hexadecimal
binary_number = 0b10101011  ## Binary literal for 171
print(f"Binary {bin(binary_number)} as hex: {hex(binary_number)}")
  1. ファイルを保存します。
  2. 次を使用してスクリプトを実行します。
python3 hex_formatting.py

次のような出力が表示されるはずです。

Decimal 171 converted to hex: 0xab
Hex 0xAB converted to decimal: 171
Hex literal 0xAB as decimal: 171
Binary 0b10101011 as hex: 0xab

出力形式の理解

ご覧のように、hex() 関数を使用して 10 進数を 16 進数に変換すると、Python は 0x プレフィックスが付いた文字列を返します。これは、多くのプログラミング言語における 16 進数の標準的な表現です。

小文字 (a-f) は、Python の 16 進数出力のデフォルトですが、アプリケーションによっては大文字または異なる形式が必要になる場合があります。

いくつかの基本的なフォーマットオプションを探索するために、スクリプトを変更しましょう。

  1. hex_formatting.py ファイルに次のコードを追加します。
## Controlling the case (uppercase/lowercase)
decimal_value = 171

## Default (lowercase)
hex_lowercase = hex(decimal_value)
print(f"\nDefault hex (lowercase): {hex_lowercase}")

## Using string methods to convert to uppercase
hex_uppercase = hex(decimal_value).upper()
print(f"Hex in uppercase: {hex_uppercase}")

## Removing the 0x prefix
hex_no_prefix = hex(decimal_value)[2:]  ## Slicing the string to remove first 2 chars
print(f"Hex without 0x prefix: {hex_no_prefix}")

## Adding padding for consistent width (2 digits with leading zeros)
hex_padded = f"{decimal_value:02x}"  ## Format specifier for 2-digit hex
print(f"Hex padded to 2 digits: {hex_padded}")

## 4-digit hex with leading zeros
hex_padded_4 = f"{decimal_value:04x}"
print(f"Hex padded to 4 digits: {hex_padded_4}")
  1. ファイルを保存します。
  2. スクリプトをもう一度実行します。
python3 hex_formatting.py

新しい出力セクションは次のようになります。

Default hex (lowercase): 0xab
Hex in uppercase: 0XAB
Hex without 0x prefix: ab
Hex padded to 2 digits: ab
Hex padded to 4 digits: 00ab

これは、Python で 16 進数出力形式を操作するいくつかの簡単な方法を示しています。次のステップでは、より高度なフォーマットオプションを探索します。

高度な 16 進数フォーマット技術

16 進数の変換と単純なフォーマットの基本を理解したところで、より高度なフォーマット技術を探求しましょう。Python は、16 進数の値に適用できる強力な文字列フォーマット機能を備えています。

文字列フォーマットメソッドの使用

さまざまなフォーマットオプションを探索するために、新しいファイルを作成しましょう。

  1. WebIDE で、/home/labex/project ディレクトリに advanced_hex_formatting.py という名前の新しいファイルを作成します。
  2. ファイルに次のコードを追加します。
## Advanced hexadecimal formatting techniques in Python

decimal_value = 171  ## This will be our example number throughout

print("Different ways to format hexadecimal output in Python:")
print("-" * 50)

## 1. Using format() function
print("\n1. Using the format() function:")
## Basic format (lowercase, no prefix)
print(f"Basic format: {format(decimal_value, 'x')}")
## Uppercase format
print(f"Uppercase: {format(decimal_value, 'X')}")
## With 0x prefix
print(f"With 0x prefix: {format(decimal_value, '#x')}")
## With 0X prefix and uppercase
print(f"With 0X prefix and uppercase: {format(decimal_value, '#X')}")
## Padding to 4 digits
print(f"Padded to 4 digits: {format(decimal_value, '04x')}")
## Padding with spaces
print(f"Padded with spaces: {format(decimal_value, '6x')}")

## 2. Using f-strings (Python 3.6+)
print("\n2. Using f-strings:")
## Basic hex
print(f"Basic format: {decimal_value:x}")
## Uppercase
print(f"Uppercase: {decimal_value:X}")
## With 0x prefix
print(f"With 0x prefix: {decimal_value:#x}")
## With 0X prefix and uppercase
print(f"With 0X prefix and uppercase: {decimal_value:#X}")
## Padding to 4 digits with zeros
print(f"Padded to 4 digits: {decimal_value:04x}")
## Centered in a 10-character field
print(f"Centered: {decimal_value:^10x}")

## 3. Using the str.format() method
print("\n3. Using str.format() method:")
## Basic format
print("Basic format: {:x}".format(decimal_value))
## Uppercase with 0X prefix
print("Uppercase with prefix: {:#X}".format(decimal_value))
## Padding with different align options
print("Right-aligned in 6 spaces: {:>6x}".format(decimal_value))
print("Left-aligned in 6 spaces: {:<6x}".format(decimal_value))
  1. ファイルを保存します。
  2. スクリプトを実行します。
python3 advanced_hex_formatting.py

出力には、さまざまな 16 進数のフォーマットオプションが表示されます。

Different ways to format hexadecimal output in Python:
--------------------------------------------------

1. Using the format() function:
Basic format: ab
Uppercase: AB
With 0x prefix: 0xab
With 0X prefix and uppercase: 0XAB
Padded to 4 digits: 00ab
Padded with spaces:     ab

2. Using f-strings:
Basic format: ab
Uppercase: AB
With 0x prefix: 0xab
With 0X prefix and uppercase: 0XAB
Padded to 4 digits: 00ab
Centered:     ab

3. Using str.format() method:
Basic format: ab
Uppercase with prefix: 0XAB
Right-aligned in 6 spaces:     ab
Left-aligned in 6 spaces: ab

バイト配列の 16 進数ダンプの作成

16 進数フォーマットの一般的な用途の 1 つは、バイナリデータの 16 進数ダンプを作成することです。バイト配列の 16 進数ダンプを表示する簡単な関数を作成しましょう。

  1. advanced_hex_formatting.py ファイルに次のコードを追加します。
## 4. Creating a hex dump of binary data
print("\n4. Creating a hex dump of binary data:")

def hex_dump(data, bytes_per_line=16):
    """Display a hex dump of binary data with both hex and ASCII representation."""
    result = []

    for i in range(0, len(data), bytes_per_line):
        chunk = data[i:i+bytes_per_line]
        ## Create the hex representation
        hex_repr = ' '.join(f'{b:02x}' for b in chunk)

        ## Create the ASCII representation (printable chars only)
        ascii_repr = ''.join(chr(b) if 32 <= b <= 126 else '.' for b in chunk)

        ## Format the line with address, hex values, and ASCII representation
        line = f"{i:04x}: {hex_repr:<{bytes_per_line*3}} {ascii_repr}"
        result.append(line)

    return '\n'.join(result)

## Example: Creating some sample binary data
sample_data = bytes([
    0x48, 0x65, 0x6C, 0x6C, 0x6f, 0x20, 0x57, 0x6f, 0x72, 0x6c, 0x64, 0x21,  ## "Hello World!"
    0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B,  ## Control characters
    0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x7F                                ## More control chars
])

## Print the hex dump
print(hex_dump(sample_data))
  1. ファイルを保存します。
  2. スクリプトをもう一度実行します。
python3 advanced_hex_formatting.py

出力の新しいセクションには、バイト配列の 16 進数ダンプが表示されます。

4. Creating a hex dump of binary data:
0000: 48 65 6c 6c 6f 20 57 6f 72 6c 64 21 00 01 02 03 Hello World!....
0010: 04 05 06 07 08 09 0a 0b 1a 1b 1c 1d 1e 1f 7f    ...............

この 16 進数ダンプ形式は、バイナリデータ、ネットワークパケット、またはファイルの内容を検査する際に一般的に使用されます。各行には以下が表示されます。

  • 16 進数でのアドレス/オフセット
  • 各バイトの 16 進数の値
  • ASCII 表現 (表示可能な文字が直接表示され、表示不可能な文字がドットとして表示されます)

これらの高度なフォーマット技術は、Python プログラムで 16 進数データを扱うための強力なツールを提供します。

Python での 16 進数の実用的な応用

Python で 16 進数の値をフォーマットする方法を学んだので、16 進数フォーマットが特に役立ついくつかの実用的な応用例を探求しましょう。

1. RGB カラー値の操作

16 進数は、Web 開発やグラフィックプログラミングで色を表すために一般的に使用されます。RGB カラー値は、#RRGGBB の形式で 16 進数の三つ組として表現できます。

16 進数を使用して色を操作する方法を示すスクリプトを作成しましょう。

  1. WebIDE で、/home/labex/project ディレクトリに hex_color_converter.py という名前の新しいファイルを作成します。
  2. ファイルに次のコードを追加します。
## Working with RGB color values using hexadecimal

def rgb_to_hex(r, g, b):
    """Convert RGB values to a hexadecimal color string."""
    ## Ensure values are within valid range (0-255)
    r = max(0, min(255, r))
    g = max(0, min(255, g))
    b = max(0, min(255, b))

    ## Create hex string with ## prefix
    return f"#{r:02x}{g:02x}{b:02x}"

def hex_to_rgb(hex_color):
    """Convert a hexadecimal color string to RGB values."""
    ## Remove ## if present
    hex_color = hex_color.lstrip('#')

    ## Convert hex to decimal
    if len(hex_color) == 3:  ## Handle shorthand hex (#RGB)
        r = int(hex_color[0] + hex_color[0], 16)
        g = int(hex_color[1] + hex_color[1], 16)
        b = int(hex_color[2] + hex_color[2], 16)
    else:  ## Handle full hex (#RRGGBB)
        r = int(hex_color[0:2], 16)
        g = int(hex_color[2:4], 16)
        b = int(hex_color[4:6], 16)

    return (r, g, b)

## Example usage
print("RGB to Hex Color Conversion Examples:")
print("-" * 40)

## Common colors
colors = [
    (255, 0, 0),      ## Red
    (0, 255, 0),      ## Green
    (0, 0, 255),      ## Blue
    (255, 255, 0),    ## Yellow
    (255, 0, 255),    ## Magenta
    (0, 255, 255),    ## Cyan
    (255, 255, 255),  ## White
    (0, 0, 0)         ## Black
]

## Convert and display each color
for rgb in colors:
    hex_color = rgb_to_hex(*rgb)
    print(f"RGB: {rgb} → Hex: {hex_color}")

print("\nHex to RGB Color Conversion Examples:")
print("-" * 40)

## List of hex colors to convert
hex_colors = ["#ff0000", "#00ff00", "#0000ff", "#ff0", "#f0f", "#0ff"]

for hex_color in hex_colors:
    rgb = hex_to_rgb(hex_color)
    print(f"Hex: {hex_color} → RGB: {rgb}")

## Color blending example
print("\nColor Blending Example:")
print("-" * 40)

def blend_colors(color1, color2, ratio=0.5):
    """Blend two colors according to the given ratio."""
    r1, g1, b1 = hex_to_rgb(color1)
    r2, g2, b2 = hex_to_rgb(color2)

    r = int(r1 * (1 - ratio) + r2 * ratio)
    g = int(g1 * (1 - ratio) + g2 * ratio)
    b = int(b1 * (1 - ratio) + b2 * ratio)

    return rgb_to_hex(r, g, b)

## Blend red and blue with different ratios
red = "#ff0000"
blue = "#0000ff"

print(f"Color 1: {red} (Red)")
print(f"Color 2: {blue} (Blue)")

for i in range(0, 11, 2):
    ratio = i / 10
    blended = blend_colors(red, blue, ratio)
    print(f"Blend ratio {ratio:.1f}: {blended}")
  1. ファイルを保存します。
  2. スクリプトを実行します。
python3 hex_color_converter.py

出力には、RGB と 16 進数のカラー形式間の変換が表示されます。

RGB to Hex Color Conversion Examples:
----------------------------------------
RGB: (255, 0, 0) → Hex: #ff0000
RGB: (0, 255, 0) → Hex: #00ff00
RGB: (0, 0, 255) → Hex: #0000ff
RGB: (255, 255, 0) → Hex: #ffff00
RGB: (255, 0, 255) → Hex: #ff00ff
RGB: (0, 255, 255) → Hex: #00ffff
RGB: (255, 255, 255) → Hex: #ffffff
RGB: (0, 0, 0) → Hex: #000000

Hex to RGB Color Conversion Examples:
----------------------------------------
Hex: #ff0000 → RGB: (255, 0, 0)
Hex: #00ff00 → RGB: (0, 255, 0)
Hex: #0000ff → RGB: (0, 0, 255)
Hex: #ff0 → RGB: (255, 255, 0)
Hex: #f0f → RGB: (255, 0, 255)
Hex: #0ff → RGB: (0, 255, 255)

Color Blending Example:
----------------------------------------
Color 1: #ff0000 (Red)
Color 2: #0000ff (Blue)
Blend ratio 0.0: #ff0000
Blend ratio 0.2: #cc0033
Blend ratio 0.4: #990066
Blend ratio 0.6: #660099
Blend ratio 0.8: #3300cc
Blend ratio 1.0: #0000ff

2. バイナリデータとファイル操作の操作

16 進数は、ファイルの内容やネットワークパケットなど、バイナリデータを扱う際に頻繁に使用されます。簡単な 16 進数のファイルビューアを作成しましょう。

  1. WebIDE で、/home/labex/project ディレクトリに hex_file_viewer.py という名前の新しいファイルを作成します。
  2. ファイルに次のコードを追加します。
## Hexadecimal file viewer

def hex_dump(data, bytes_per_line=16, offset=0):
    """Create a hex dump of binary data."""
    result = []

    for i in range(0, len(data), bytes_per_line):
        chunk = data[i:i+bytes_per_line]
        ## Hex representation
        hex_part = ' '.join(f'{b:02x}' for b in chunk)
        ## ASCII representation
        ascii_part = ''.join(chr(b) if 32 <= b <= 126 else '.' for b in chunk)

        ## Add line to result
        line = f"{offset+i:08x}:  {hex_part:<{bytes_per_line*3}}  |{ascii_part}|"
        result.append(line)

    return '\n'.join(result)

def create_sample_file():
    """Create a sample binary file for demonstration."""
    file_path = "/home/labex/project/sample.bin"

    ## Create some sample data containing:
    ## - Some text
    ## - A range of values from 0-255
    sample_data = bytearray(b"This is a sample binary file.\n")
    sample_data.extend(range(0, 256))

    ## Write to file
    with open(file_path, 'wb') as f:
        f.write(sample_data)

    return file_path

def view_file_as_hex(file_path, max_bytes=256):
    """View a portion of the file as a hex dump."""
    try:
        with open(file_path, 'rb') as f:
            data = f.read(max_bytes)

        print(f"Hex dump of the first {len(data)} bytes of {file_path}:")
        print("-" * 70)
        print(hex_dump(data))

        if max_bytes < 256:
            print(f"\nOnly showing first {max_bytes} bytes. Adjust max_bytes parameter to see more.")

    except FileNotFoundError:
        print(f"Error: File '{file_path}' not found.")
    except Exception as e:
        print(f"Error reading file: {e}")

## Create a sample file and view it
sample_file = create_sample_file()
view_file_as_hex(sample_file, 128)

## Information about the "hex" command in Linux
print("\nFun fact: In Linux, you can use the 'hexdump' or 'xxd' commands")
print("to view binary files in hexadecimal format directly from the terminal.")
  1. ファイルを保存します。
  2. スクリプトを実行します。
python3 hex_file_viewer.py

出力には、サンプルバイナリファイルの 16 進数ダンプが表示されます。

Hex dump of the first 128 bytes of /home/labex/project/sample.bin:
----------------------------------------------------------------------
00000000:  54 68 69 73 20 69 73 20 61 20 73 61 6d 70 6c 65  |This is a sample|
00000010:  20 62 69 6e 61 72 79 20 66 69 6c 65 2e 0a 00 01  | binary file...|
00000020:  02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11  |................|
00000030:  12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f 20 21  |.............. !|
00000040:  22 23 24 25 26 27 28 29 2a 2b 2c 2d 2e 2f 30 31  |"#$%&'()*+,-./01|
00000050:  32 33 34 35 36 37 38 39 3a 3b 3c 3d 3e 3f 40 41  |23456789:;<=>?@A|
00000060:  42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f 50 51  |BCDEFGHIJKLMNOPQ|
00000070:  52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f 60 61  |RSTUVWXYZ[\]^_`a|

Fun fact: In Linux, you can use the 'hexdump' or 'xxd' commands
to view binary files in hexadecimal format directly from the terminal.

この 16 進数ビューアは、各バイトの 16 進数表現とその ASCII 表現の両方を表示します。これは、バイナリデータを検査するための一般的な形式です。

これらの実用的な例は、16 進数フォーマットが実際の Python アプリケーションでどのように役立つかを示しています。色の操作からバイナリデータの分析まで、16 進数は、さまざまな種類のデータを表現し、操作するためのコンパクトで効率的な方法を提供します。

まとめ

このチュートリアルでは、以下を学習しました。

  • 16 進数(hexadecimal number system)の基本と、2 進数(binary)および 10 進数(decimal)との関係
  • Python で 10 進数と 16 進数の値を相互に変換する方法
  • 16 進数出力をフォーマットするためのさまざまなテクニック。これには以下が含まれます。
    • hex() 関数を使用した基本的な変換
    • さまざまなプレフィックス、パディング、および大文字/小文字オプションを使用したフォーマット
    • バイナリデータのフォーマットされた 16 進数ダンプの作成
  • Python での 16 進数の実用的な応用:
    • Web およびグラフィック開発における RGB カラー値の操作
    • 16 進数ダンプを使用したバイナリデータとファイル内容の分析

16 進数フォーマットを理解することは、多くのプログラミングタスク、特にローレベルの操作、バイナリデータ、またはコンパクトな表現が重要な領域を扱う場合に不可欠なスキルです。このチュートリアルで学習したテクニックは、Python プログラムでさまざまなデータ形式をより効率的に操作するのに役立ちます。

この分野の知識をさらに深めるには、以下を検討することをお勧めします。

  • Python でのビット単位演算(bitwise operations)
  • ネットワークプログラミングとパケット分析
  • コンピュータグラフィックスと画像処理
  • メモリ管理とシステムプログラミング