使用Coden模組進行二進位制、十六進位制和十進位制數之間的轉換


coden 是Tanmay Earappa開發的一個Python庫,用於秘密程式碼(解碼和編碼秘密程式碼)。此模組提供執行程式碼轉換的功能。以下是此模組提供的一些函式及其功能:

  • coden.secret(): 根據mode引數的輸入,用於解碼或編碼秘密程式碼。

  • hex_to_bin(): 執行十六進位制到二進位制的轉換。

  • int_to_bin(): 執行十進位制到二進位制的轉換。

  • int_to_hex(): 執行十進位制到十六進位制的轉換

使用pip安裝Coden

使用pip命令可以輕鬆安裝此模組,然後透過匯入模組,可以訪問Python直譯器中的轉換功能。只需在命令提示符中執行以下命令,即可獲得Coden模組。

pip install --coden

在本文中,我們將討論使用Coden模組進行二進位制、十六進位制和十進位制數之間的轉換

十六進位制到二進位制轉換

使用hex_to_bin()函式可以將十六進位制數轉換為二進位制數。

示例

輸入的十六進位制數為f1ff,預期的二進位制數為1111000111111111

# importing the coden module
import coden

hexadecimal_number="f1ff" 
print("Input Hexadecimal Number:", hexadecimal_number)

binary_output = coden.hex_to_bin(hexadecimal_number)
print('Binary Output:', binary_output)

輸出

Input Hexadecimal Number: f1ff
Binary Output: 1111000111111111

十六進位制到十進位制轉換

函式coden.hex_to_int() 將十六進位制數轉換為十進位制數。

示例

讓我們取一個十六進位制數並將其轉換為十進位制數。

import coden

hexadecimal_number="f1ff63" 
print("Input Hexadecimal Number:", hexadecimal_number)

decimal_number = coden.hex_to_int(hexadecimal_number)
print('Decimal Output:', decimal_number)

輸出

Input Hexadecimal Number: f1ff63
Decimal Output: 15859555

二進位制到十六進位制轉換

函式coden.bin_to_hex() 將二進位制數轉換為十六進位制數。

示例

這裡我們將取一個二進位制數,預期的十六進位制數將是abc123efff。

import coden

binary_number = '001010101111000001001000111110111111111111'
print("Input Binary Number:", binary_number)

# Convert Binary Number to Hexadecimal Number
hexadecimal_output = coden.bin_to_hex(binary_number)
print('Hexadecimal Output:', hexadecimal_output)

輸出

Input Binary Number: 001010101111000001001000111110111111111111
Hexadecimal Output: abc123efff

十進位制到二進位制轉換

使用coden.int_to_bin()函式可以將十進位制數轉換為二進位制數。

示例

讓我們取一個十進位制數並將其轉換為二進位制數。

import coden
  
decimal_number = 16227
print("Input decimal Number:", decimal_number)

binary_output = coden.int_to_bin(decimal_number)
print('Binary Output',binary_output)

輸出

Input decimal Number: 16227
Binary Output 11111101100011

十進位制到十六進位制轉換

使用int_to_hex()函式可以將十進位制數轉換為十六進位制數。

示例

讓我們取一個十進位制數並將其轉換為十六進位制數。

import coden
  
decimal_number = 16227
print("Input decimal Number:", decimal_number)

hexadecimal_output = coden.int_to_hex(decimal_number)
print('Hexadecimal Output',hexadecimal_output)

輸出

Input decimal Number: 16227
Hexadecimal Output 3f63

二進位制到十進位制轉換

coden.bin_to_int()函式將執行二進位制到十進位制的轉換。

示例

讓我們取一個二進位制數,並使用bin_to_int()方法將其轉換為十進位制數。

import coden

binary_number = '001010101111000001001000111110111111111111'
print("Input Binary Number:", binary_number)

decimal_output = coden.bin_to_int(binary_number)
print('Decimal Output:', decimal_output)

輸出

Input Binary Number: 001010101111000001001000111110111111111111
Decimal Output: 737679765503

我們已經討論了所有與二進位制、十六進位制和十進位制數之間程式碼轉換相關的coden方法。

更新於:2023年5月30日

瀏覽量:147

啟動您的職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.