10 進数を 16 進数に変換する

PythonPythonBeginner
今すぐ練習

This tutorial is from open-source community. Access the source code

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

はじめに

Python では、hex() 関数を使って、10進数を16進数に簡単に変換できます。このチャレンジでは、10進数を引数として受け取り、その16進数表記を返す関数を書くよう求められます。


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("Python")) -.-> python/BasicConceptsGroup(["Basic Concepts"]) python(("Python")) -.-> python/FunctionsGroup(["Functions"]) python/BasicConceptsGroup -.-> python/comments("Comments") python/FunctionsGroup -.-> python/function_definition("Function Definition") python/FunctionsGroup -.-> python/build_in_functions("Build-in Functions") subgraph Lab Skills python/comments -.-> lab-13732{{"10 進数を 16 進数に変換する"}} python/function_definition -.-> lab-13732{{"10 進数を 16 進数に変換する"}} python/build_in_functions -.-> lab-13732{{"10 進数を 16 進数に変換する"}} end

10進数を16進数に変換する

10進数を引数として受け取り、その16進数表記を返す to_hex(dec) 関数を書きましょう。この関数は次の手順を実行する必要があります。

  1. hex() を使って10進数を16進数に変換します。
  2. 16進数表記を返します。
def to_hex(dec):
  return hex(dec)
to_hex(41) ## 0x29
to_hex(332) ## 0x14c

まとめ

このチャレンジでは、Python の hex() 関数を使って10進数を16進数に変換する方法を学びました。また、この変換を行う関数も作成しました。