Hướng dẫn python octal to decimal - trăn bát phân sang thập phân

Hệ thống số bát diện, hoặc viết tắt là OCT, là hệ thống số cơ sở 8 và sử dụng các chữ số 0 đến 7. Đặc điểm chính của hệ thống đánh số bát phân là chỉ có 8 chữ số đếm riêng biệt từ & nbsp; 0 & nbsp; đến & nbsp; 7 & nbsp; với mỗi chữ số có trọng lượng hoặc giá trị chỉ 8 bắt đầu từ bit ít có ý nghĩa nhất [LSB].0 to 7 with each digit having a weight or value of just 8 starting from the least significant bit [LSB].

Trong hệ thống số thập phân, mỗi chữ số đại diện cho sức mạnh khác nhau của 10 tạo ra chúng là hệ thống số cơ sở-10.

Định nghĩa vấn đề

Tạo một chương trình Python để chuyển đổi một số bát phân thành số thập phân.

Thuật toán để chuyển đổi thập phân sang số thập phân

  1. Lấy một số bát phân làm đầu vào.
  2. Nhân mỗi chữ số của số octal bắt đầu từ số cuối với sức mạnh tương ứng.
  3. Thêm tất cả các chữ số nhân.
  4. Tổng số tiền cho số thập phân.

Chương trình

num = input["Enter an octal number :"]

OctalToDecimal[int[num]]

def OctalToDecimal[num]:

    decimal_value = 0
    base = 1

    while [num]:
        last_digit = num % 10
        num = int[num / 10]
        decimal_value += last_digit * base
        base = base * 8
    print["The decimal value is :",decimal_value]

Đầu ra

Enter an octal number :24
The decimal value is :20

Trong Python, có một cách khác để chuyển đổi số octal thành số thập phân bằng phương pháp

Enter an octal number :24
The decimal value of 24 is 20
5.

Chương trình

octal_num = input["Enter an octal number :"]
decimal_value = int[octal_num,8]
print["The decimal value of {} is {}".format[octal_num,decimal_value]]

Đầu ra

Enter an octal number :24
The decimal value of 24 is 20

Định nghĩa vấn đề

Tạo một chương trình Python để chuyển đổi một số bát phân thành số thập phân.

Thuật toán để chuyển đổi thập phân sang số thập phân

  1. Lấy một số bát phân làm đầu vào.
  2. Nhân mỗi chữ số của số octal bắt đầu từ số cuối với sức mạnh tương ứng.
  3. Thêm tất cả các chữ số nhân.
  4. Tổng số tiền cho số thập phân.

Chương trình

decimal = int[input["Enter a decimal number :"]]

print["The octal equivalent is :",decimal_to_octal[decimal]]

def decimal_to_octal[decimal]:
    octal = 0
    i = 1
    while [decimal != 0]:
        octal = octal + [decimal % 8] * i
        decimal = int[decimal / 8]
        i = i * 10
    return octal

Đầu ra

Trong Python, có một cách khác để chuyển đổi số octal thành số thập phân bằng phương pháp
Enter an octal number :24
The decimal value of 24 is 20
5.

Tạo một chương trình Python để chuyển đổi số thập phân thành một số bát phân.

Chương trình

decimal_number = int[input["Enter a decimal number :"]]
octal_number = oct[decimal_number].replace["0o", ""]
print["The octal value for {} is {}".format[decimal_number,octal_number ]]

Đầu ra

Enter a decimal number :200
The octal value for 200 is 310

Trong Python, có một cách khác để chuyển đổi số octal thành số thập phân bằng phương pháp

Enter an octal number :24
The decimal value of 24 is 20
5.

  • Tạo một chương trình Python để chuyển đổi số thập phân thành một số bát phân.

Thuật toán

Cập nhật lần cuối vào ngày 19 tháng 8 năm 2022 21:51:43 [UTC/GMT +8 giờ]

Chuỗi Python: Tập thể dục-83 với giải pháp

Viết một chương trình Python để in bốn giá trị thập phân, bát phân, thập lục phân [viết hoa], nhị phân trong một dòng duy nhất của một số nguyên nhất định.

Nội phân chính

  • Chuỗi Python: Tập thể dục-83 với giải pháp
  • Viết một chương trình Python để in bốn giá trị thập phân, bát phân, thập lục phân [viết hoa], nhị phân trong một dòng duy nhất của một số nguyên nhất định.
  • Nội phân chính
  • Trực quan hóa thực thi mã Python:
  • Mã nguồn
  • Viết số nguyên bằng ký hiệu nhị phân, bát phân và thập lục phân
  • Chuyển đổi một số thành chuỗi nhị phân, bát phân và thập lục phân
  • Bin [], Oct [], Hex []
  • Chuyển đổi chuỗi nhị phân, bát phân và thập lục phân thành một số
  • Ví dụ sử dụng
  • Chuỗi nhị phân số học
  • Chuyển đổi giữa các số nhị phân, bát phân và thập lục phân
  • Làm thế nào để bạn in nhị phân hình lục phân thập phân thập phân trong Python?
  • Làm thế nào để bạn đại diện cho Octal và thập lục phân trong Python?
  • Hexadecimal thập phân nhị phân nhị phân là gì?

Một số octal với cơ sở 8 sử dụng tám chữ số 0,1,2,3,4,5,6 và 7. Một biểu diễn nhỏ gọn hơn được sử dụng bởi biểu diễn thập lục phân mà nhóm 4 chữ số nhị phân với nhau. Nó có thể sử dụng 16 chữ số, nhưng vì chúng ta chỉ có 10 chữ số, 6 chữ số còn lại được tạo thành từ 6 chữ cái đầu tiên của bảng chữ cái.:-

Cập nhật lần cuối vào ngày 19 tháng 8 năm 2022 21:51:43 [UTC/GMT +8 giờ]

i = int[input["Input an integer: "]]
o = str[oct[i]][2:]
h = str[hex[i]][2:]
h = h.upper[]
b = str[bin[i]][2:]
d = str[i]
print["Decimal Octal Hexadecimal [capitalized], Binary"]
print[d,'  ',o,' ',h,'                   ',b]

Chuỗi Python: Tập thể dục-83 với giải pháp

Input an integer:  25
Decimal Octal Hexadecimal [capitalized], Binary
25    31   19                     11001

Flowchart:


Viết một chương trình Python để in bốn giá trị thập phân, bát phân, thập lục phân [viết hoa], nhị phân trong một dòng duy nhất của một số nguyên nhất định.

Nội phân chính

Trực quan hóa thực thi mã Python:

Mã nguồn

Viết số nguyên bằng ký hiệu nhị phân, bát phân và thập lục phân Write a Python program to wrap a given string into a paragraph of given width.
Next: Write a Python program to swap cases of a given string.

Chuyển đổi một số thành chuỗi nhị phân, bát phân và thập lục phân

Bin [], Oct [], Hex []

  • định dạng [], str.format [], f-string

Chuyển đổi số nguyên âm thành một chuỗi trong biểu diễn bổ sung của hai

Giải pháp mẫu:-

Một số có tiền tố ________ 37 & nbsp; được coi là nhị phân, ________ 38 & nbsp; được coi là octal và ____ 39 & nbsp; như hình lục giác. Ví dụ:

Enter an octal number :24
The decimal value is :20
0

Mã nguồn

Enter an octal number :24
The decimal value is :20
1

Đầu ra

Enter an octal number :24
The decimal value is :20
2

Lưu ý: Để kiểm tra chương trình cho các số thập phân khác, thay đổi giá trị của DEC trong chương trình. To test the program for other decimal numbers, change the value of dec in the program.

Trong chương trình này, chúng tôi đã sử dụng các hàm tích hợp

decimal = int[input["Enter a decimal number :"]]

print["The octal equivalent is :",decimal_to_octal[decimal]]

def decimal_to_octal[decimal]:
    octal = 0
    i = 1
    while [decimal != 0]:
        octal = octal + [decimal % 8] * i
        decimal = int[decimal / 8]
        i = i * 10
    return octal
0,
Enter an octal number :24
The decimal value of 24 is 20
6 và
decimal = int[input["Enter a decimal number :"]]

print["The octal equivalent is :",decimal_to_octal[decimal]]

def decimal_to_octal[decimal]:
    octal = 0
    i = 1
    while [decimal != 0]:
        octal = octal + [decimal % 8] * i
        decimal = int[decimal / 8]
        i = i * 10
    return octal
2 để chuyển đổi số thập phân đã cho thành các hệ thống số tương ứng.

Các hàm này có một số nguyên [theo số thập phân] và trả về một chuỗi.

Trong Python, bạn có thể xử lý các số và chuỗi dưới dạng nhị phân [thùng], bát phân [OCT] và thập lục phân [lục giác] cũng như thập phân. Họ cũng có thể được chuyển đổi sang nhau.

Bài viết này mô tả các nội dung sau đây.

  • Viết số nguyên bằng ký hiệu nhị phân, bát phân và thập lục phân
  • Chuyển đổi một số thành chuỗi nhị phân, bát phân và thập lục phân
    • decimal = int[input["Enter a decimal number :"]]
      
      print["The octal equivalent is :",decimal_to_octal[decimal]]
      
      def decimal_to_octal[decimal]:
          octal = 0
          i = 1
          while [decimal != 0]:
              octal = octal + [decimal % 8] * i
              decimal = int[decimal / 8]
              i = i * 10
          return octal
      0,
      Enter an octal number :24
      The decimal value of 24 is 20
      6,
      decimal = int[input["Enter a decimal number :"]]
      
      print["The octal equivalent is :",decimal_to_octal[decimal]]
      
      def decimal_to_octal[decimal]:
          octal = 0
          i = 1
          while [decimal != 0]:
              octal = octal + [decimal % 8] * i
              decimal = int[decimal / 8]
              i = i * 10
          return octal
      2
    • decimal = int[input["Enter a decimal number :"]]
      
      print["The octal equivalent is :",decimal_to_octal[decimal]]
      
      def decimal_to_octal[decimal]:
          octal = 0
          i = 1
          while [decimal != 0]:
              octal = octal + [decimal % 8] * i
              decimal = int[decimal / 8]
              i = i * 10
          return octal
      6,
      decimal = int[input["Enter a decimal number :"]]
      
      print["The octal equivalent is :",decimal_to_octal[decimal]]
      
      def decimal_to_octal[decimal]:
          octal = 0
          i = 1
          while [decimal != 0]:
              octal = octal + [decimal % 8] * i
              decimal = int[decimal / 8]
              i = i * 10
          return octal
      7, F-String
    • Chuyển đổi số nguyên âm thành một chuỗi trong biểu diễn bổ sung của hai
  • Chuyển đổi chuỗi nhị phân, bát phân và thập lục phân thành một số
    • Enter an octal number :24
      The decimal value of 24 is 20
      5
  • Ví dụ sử dụng
    • Chuỗi nhị phân số học
    • Chuyển đổi giữa các số nhị phân, bát phân và thập lục phân

Xem bài viết sau đây về những điều cơ bản về chuyển đổi giữa chuỗi [

decimal = int[input["Enter a decimal number :"]]

print["The octal equivalent is :",decimal_to_octal[decimal]]

def decimal_to_octal[decimal]:
    octal = 0
    i = 1
    while [decimal != 0]:
        octal = octal + [decimal % 8] * i
        decimal = int[decimal / 8]
        i = i * 10
    return octal
9] và số [
Enter a decimal number :200
The octal equivalent is : 310
0,
Enter a decimal number :200
The octal equivalent is : 310
1].

  • Chuyển đổi một chuỗi thành một số [int, float] trong python

Viết số nguyên bằng ký hiệu nhị phân, bát phân và thập lục phân

Chuyển đổi một số thành chuỗi nhị phân, bát phân và thập lục phân

Enter an octal number :24
The decimal value is :20
3

decimal = int[input["Enter a decimal number :"]]

print["The octal equivalent is :",decimal_to_octal[decimal]]

def decimal_to_octal[decimal]:
    octal = 0
    i = 1
    while [decimal != 0]:
        octal = octal + [decimal % 8] * i
        decimal = int[decimal / 8]
        i = i * 10
    return octal
0,
Enter an octal number :24
The decimal value of 24 is 20
6,
decimal = int[input["Enter a decimal number :"]]

print["The octal equivalent is :",decimal_to_octal[decimal]]

def decimal_to_octal[decimal]:
    octal = 0
    i = 1
    while [decimal != 0]:
        octal = octal + [decimal % 8] * i
        decimal = int[decimal / 8]
        i = i * 10
    return octal
2

Enter an octal number :24
The decimal value is :20
4

decimal = int[input["Enter a decimal number :"]]

print["The octal equivalent is :",decimal_to_octal[decimal]]

def decimal_to_octal[decimal]:
    octal = 0
    i = 1
    while [decimal != 0]:
        octal = octal + [decimal % 8] * i
        decimal = int[decimal / 8]
        i = i * 10
    return octal
6,
decimal = int[input["Enter a decimal number :"]]

print["The octal equivalent is :",decimal_to_octal[decimal]]

def decimal_to_octal[decimal]:
    octal = 0
    i = 1
    while [decimal != 0]:
        octal = octal + [decimal % 8] * i
        decimal = int[decimal / 8]
        i = i * 10
    return octal
7, F-String

Enter an octal number :24
The decimal value is :20
5

Chuyển đổi số nguyên âm thành một chuỗi trong biểu diễn bổ sung của hai

Enter an octal number :24
The decimal value is :20
6

Chuyển đổi chuỗi nhị phân, bát phân và thập lục phân thành một số

Ví dụ sử dụng

Enter an octal number :24
The decimal value is :20
7

Chuỗi nhị phân số học

Chuyển đổi giữa các số nhị phân, bát phân và thập lục phân

  • Xem bài viết sau đây về những điều cơ bản về chuyển đổi giữa chuỗi [
    decimal = int[input["Enter a decimal number :"]]
    
    print["The octal equivalent is :",decimal_to_octal[decimal]]
    
    def decimal_to_octal[decimal]:
        octal = 0
        i = 1
        while [decimal != 0]:
            octal = octal + [decimal % 8] * i
            decimal = int[decimal / 8]
            i = i * 10
        return octal
    9] và số [
    Enter a decimal number :200
    The octal equivalent is : 310
    
    0,
    Enter a decimal number :200
    The octal equivalent is : 310
    
    1].
  • Chuyển đổi một chuỗi thành một số [int, float] trong python

Bằng cách thêm các tiền tố

Enter an octal number :24
The decimal value of 24 is 20
7,
Enter an octal number :24
The decimal value of 24 is 20
8 và
Enter an octal number :24
The decimal value of 24 is 20
9, số số nguyên
Enter a decimal number :200
The octal equivalent is : 310
0 có thể được viết tương ứng bằng nhị phân, bát phân và thập lục phân, tương ứng. Đầu ra của
Enter a decimal number :200
The octal equivalent is : 310
6 sẽ nằm trong ký hiệu thập phân.

Bạn cũng có thể sử dụng chữ hoa
Enter a decimal number :200
The octal equivalent is : 310
7,
Enter a decimal number :200
The octal equivalent is : 310
8 và
Enter a decimal number :200
The octal equivalent is : 310
9.

Ngay cả với tiền tố, loại là số nguyên

Enter a decimal number :200
The octal equivalent is : 310
0.

  • Vì chúng là
    Enter a decimal number :200
    The octal equivalent is : 310
    
    0, chúng có thể được vận hành như bình thường.
  • Trong Python3.6 và sau đó, bạn có thể chèn nhấn mạnh
    decimal_number = int[input["Enter a decimal number :"]]
    octal_number = oct[decimal_number].replace["0o", ""]
    print["The octal value for {} is {}".format[decimal_number,octal_number ]]
    2 trong các số. Lặp lại dấu gạch dưới
    decimal_number = int[input["Enter a decimal number :"]]
    octal_number = oct[decimal_number].replace["0o", ""]
    print["The octal value for {} is {}".format[decimal_number,octal_number ]]
    2 gây ra lỗi, nhưng bạn có thể chèn bao nhiêu tùy thích nếu nó không được lặp lại.
  • Dấu gạch dưới
    decimal_number = int[input["Enter a decimal number :"]]
    octal_number = oct[decimal_number].replace["0o", ""]
    print["The octal value for {} is {}".format[decimal_number,octal_number ]]
    2 có thể được sử dụng như một dấu phân cách khi có nhiều chữ số. Ví dụ, dễ đọc nếu bạn nhập
    decimal_number = int[input["Enter a decimal number :"]]
    octal_number = oct[decimal_number].replace["0o", ""]
    print["The octal value for {} is {}".format[decimal_number,octal_number ]]
    2 cứ sau bốn chữ số.
Enter an octal number :24
The decimal value is :20
8

Chuyển đổi một số thành chuỗi nhị phân, bát phân và thập lục phân

  • Bạn có thể sử dụng các chức năng sau để chuyển đổi một số thành chuỗi nhị phân, bát phân hoặc thập lục phân.
Enter an octal number :24
The decimal value is :20
9

Chức năng tích hợp

decimal = int[input["Enter a decimal number :"]]

print["The octal equivalent is :",decimal_to_octal[decimal]]

def decimal_to_octal[decimal]:
    octal = 0
    i = 1
    while [decimal != 0]:
        octal = octal + [decimal % 8] * i
        decimal = int[decimal / 8]
        i = i * 10
    return octal
0,
Enter an octal number :24
The decimal value of 24 is 20
6,
decimal = int[input["Enter a decimal number :"]]

print["The octal equivalent is :",decimal_to_octal[decimal]]

def decimal_to_octal[decimal]:
    octal = 0
    i = 1
    while [decimal != 0]:
        octal = octal + [decimal % 8] * i
        decimal = int[decimal / 8]
        i = i * 10
    return octal
2

  • Chức năng tích hợp
    decimal = int[input["Enter a decimal number :"]]
    
    print["The octal equivalent is :",decimal_to_octal[decimal]]
    
    def decimal_to_octal[decimal]:
        octal = 0
        i = 1
        while [decimal != 0]:
            octal = octal + [decimal % 8] * i
            decimal = int[decimal / 8]
            i = i * 10
        return octal
    6, Phương pháp chuỗi
    decimal = int[input["Enter a decimal number :"]]
    
    print["The octal equivalent is :",decimal_to_octal[decimal]]
    
    def decimal_to_octal[decimal]:
        octal = 0
        i = 1
        while [decimal != 0]:
            octal = octal + [decimal % 8] * i
            decimal = int[decimal / 8]
            i = i * 10
        return octal
    7, F-String
octal_num = input["Enter an octal number :"]
decimal_value = int[octal_num,8]
print["The decimal value of {} is {}".format[octal_num,decimal_value]]
0

Nó cũng mô tả cách lấy một chuỗi trong biểu diễn bổ sung của hai cho một giá trị âm.

Bin [], Oct [], Hex []

  • Các hàm tích hợp
    decimal = int[input["Enter a decimal number :"]]
    
    print["The octal equivalent is :",decimal_to_octal[decimal]]
    
    def decimal_to_octal[decimal]:
        octal = 0
        i = 1
        while [decimal != 0]:
            octal = octal + [decimal % 8] * i
            decimal = int[decimal / 8]
            i = i * 10
        return octal
    0,
    Enter an octal number :24
    The decimal value of 24 is 20
    6 và
    decimal = int[input["Enter a decimal number :"]]
    
    print["The octal equivalent is :",decimal_to_octal[decimal]]
    
    def decimal_to_octal[decimal]:
        octal = 0
        i = 1
        while [decimal != 0]:
            octal = octal + [decimal % 8] * i
            decimal = int[decimal / 8]
            i = i * 10
        return octal
    2 có thể được sử dụng để chuyển đổi một số thành chuỗi nhị phân, bát phân và thập lục phân. Các chức năng này trả về một chuỗi với các tiền tố ________ 37, ________ 38 và
    Enter an octal number :24
    The decimal value of 24 is 20
    9, tương ứng.

Chức năng tích hợp - Bin [] - Tài liệu Python 3.9.1

  • Chức năng tích hợp - OCT [] - Tài liệu Python 3.9.1
octal_num = input["Enter an octal number :"]
decimal_value = int[octal_num,8]
print["The decimal value of {} is {}".format[octal_num,decimal_value]]
1

Chức năng tích hợp - Hex [] - Tài liệu Python 3.9.1

octal_num = input["Enter an octal number :"]
decimal_value = int[octal_num,8]
print["The decimal value of {} is {}".format[octal_num,decimal_value]]
2

Nếu bạn không cần tiền tố, hãy sử dụng Slice

Enter a decimal number :200
The octal value for 200 is 310
7 để trích xuất các chuỗi kéo dài hoặc sử dụng
decimal = int[input["Enter a decimal number :"]]

print["The octal equivalent is :",decimal_to_octal[decimal]]

def decimal_to_octal[decimal]:
    octal = 0
    i = 1
    while [decimal != 0]:
        octal = octal + [decimal % 8] * i
        decimal = int[decimal / 8]
        i = i * 10
    return octal
6 như được mô tả tiếp theo.

octal_num = input["Enter an octal number :"]
decimal_value = int[octal_num,8]
print["The decimal value of {} is {}".format[octal_num,decimal_value]]
3

Cách cắt một danh sách, chuỗi, tuple trong Python

  • Nếu bạn muốn chuyển đổi thành chuỗi thập phân, hãy sử dụng
    Enter a decimal number :200
    The octal value for 200 is 310
    9.
octal_num = input["Enter an octal number :"]
decimal_value = int[octal_num,8]
print["The decimal value of {} is {}".format[octal_num,decimal_value]]
4

Chức năng tích hợp - Str [] - Tài liệu Python 3.9.1

  • định dạng [], str.format [], f-string
octal_num = input["Enter an octal number :"]
decimal_value = int[octal_num,8]
print["The decimal value of {} is {}".format[octal_num,decimal_value]]
5

Chuyển đổi số nguyên âm thành một chuỗi trong biểu diễn bổ sung của hai

Khi sử dụng

decimal = int[input["Enter a decimal number :"]]

print["The octal equivalent is :",decimal_to_octal[decimal]]

def decimal_to_octal[decimal]:
    octal = 0
    i = 1
    while [decimal != 0]:
        octal = octal + [decimal % 8] * i
        decimal = int[decimal / 8]
        i = i * 10
    return octal
0 hoặc
decimal = int[input["Enter a decimal number :"]]

print["The octal equivalent is :",decimal_to_octal[decimal]]

def decimal_to_octal[decimal]:
    octal = 0
    i = 1
    while [decimal != 0]:
        octal = octal + [decimal % 8] * i
        decimal = int[decimal / 8]
        i = i * 10
    return octal
6, các số nguyên âm được chuyển đổi thành các giá trị tuyệt đối với dấu trừ.

octal_num = input["Enter an octal number :"]
decimal_value = int[octal_num,8]
print["The decimal value of {} is {}".format[octal_num,decimal_value]]
6

Trong Python, các hoạt động bitwise trên các số nguyên âm được thực hiện trong biểu diễn bổ sung của hai, vì vậy nếu bạn muốn có được một chuỗi được thể hiện trong biểu diễn bổ sung của hai ] cho 4bit,

Input an integer:  25
Decimal Octal Hexadecimal [capitalized], Binary
25    31   19                     11001
8 cho 8bit và
Input an integer:  25
Decimal Octal Hexadecimal [capitalized], Binary
25    31   19                     11001
9 cho 16 bit.

  • Bổ sung của Two - Wikipedia
octal_num = input["Enter an octal number :"]
decimal_value = int[octal_num,8]
print["The decimal value of {} is {}".format[octal_num,decimal_value]]
7

Chuyển đổi chuỗi nhị phân, bát phân và thập lục phân thành một số

int[]

Sử dụng hàm tích hợp

Enter an octal number :24
The decimal value of 24 is 20
5 để chuyển đổi chuỗi nhị phân, bát phân và thập lục phân thành một số.

  • Chức năng tích hợp - Int [] - Tài liệu Python 3.9.1

Bạn có thể chuyển đổi chuỗi nhị phân, bát phân và thập lục phân thành một số nguyên dựa trên radix với

Enter an octal number :24
The decimal value is :20
01. Nếu
Enter an octal number :24
The decimal value is :20
02 bị bỏ qua, nó được coi là một số thập phân.

octal_num = input["Enter an octal number :"]
decimal_value = int[octal_num,8]
print["The decimal value of {} is {}".format[octal_num,decimal_value]]
8

Nếu bạn đặt

Enter an octal number :24
The decimal value is :20
02 thành
Input an integer:  25
Decimal Octal Hexadecimal [capitalized], Binary
25    31   19                     11001
0, nó sẽ được chuyển đổi theo tiền tố [
Enter an octal number :24
The decimal value of 24 is 20
7,
Enter an octal number :24
The decimal value of 24 is 20
8,
Enter an octal number :24
The decimal value of 24 is 20
9 hoặc
Enter a decimal number :200
The octal equivalent is : 310
7,
Enter a decimal number :200
The octal equivalent is : 310
8,
Enter a decimal number :200
The octal equivalent is : 310
9].

octal_num = input["Enter an octal number :"]
decimal_value = int[octal_num,8]
print["The decimal value of {} is {}".format[octal_num,decimal_value]]
9

Khi

Enter an octal number :24
The decimal value is :20
02 là
Input an integer:  25
Decimal Octal Hexadecimal [capitalized], Binary
25    31   19                     11001
0, một chuỗi không có tiền tố được chuyển đổi dưới dạng số thập phân. Trong trường hợp đó, lưu ý rằng một lỗi sẽ xảy ra nếu phần đầu [bên trái] được đệm với
Input an integer:  25
Decimal Octal Hexadecimal [capitalized], Binary
25    31   19                     11001
0.

Enter an octal number :24
The decimal value of 24 is 20
0

Trong các trường hợp khác, chuỗi được đệm với

Input an integer:  25
Decimal Octal Hexadecimal [capitalized], Binary
25    31   19                     11001
0 có thể được chuyển đổi.

Enter an octal number :24
The decimal value of 24 is 20
1

Một lỗi được nêu ra nếu chuỗi không thể được chuyển đổi dựa trên radix hoặc tiền tố được chỉ định.

Enter an octal number :24
The decimal value of 24 is 20
2

Ví dụ sử dụng

Chuỗi nhị phân số học

Ví dụ: nếu bạn muốn vận hành một chuỗi nhị phân với tiền tố

Enter an octal number :24
The decimal value of 24 is 20
7, bạn có thể dễ dàng chuyển đổi nó thành một số nguyên
Enter a decimal number :200
The octal equivalent is : 310
0, vận hành một cái gì đó, sau đó chuyển đổi nó trở lại chuỗi
decimal = int[input["Enter a decimal number :"]]

print["The octal equivalent is :",decimal_to_octal[decimal]]

def decimal_to_octal[decimal]:
    octal = 0
    i = 1
    while [decimal != 0]:
        octal = octal + [decimal % 8] * i
        decimal = int[decimal / 8]
        i = i * 10
    return octal
9.

Enter an octal number :24
The decimal value of 24 is 20
3

Chuyển đổi giữa các số nhị phân, bát phân và thập lục phân

Nó cũng dễ dàng chuyển đổi chuỗi nhị phân, bát phân và thập lục phân cho nhau. Sau khi chuyển đổi thành số

Enter a decimal number :200
The octal equivalent is : 310
0, nó có thể được chuyển đổi thành một chuỗi của bất kỳ định dạng nào.

Không có padding và tiền tố có thể được kiểm soát bằng chuỗi đặc tả định dạng.

Enter an octal number :24
The decimal value of 24 is 20
4

Làm thế nào để bạn in nhị phân hình lục phân thập phân thập phân trong Python?

Mã nguồn # Chương trình Python để chuyển đổi thập phân thành các hệ thống số khác dec = 344 in ["Giá trị thập phân của", dec, "là:"] in [bin [dec], "in nhị phân."] , "Trong Octal."] In [hex [dec], "trong thập lục phân."] Giá trị thập phân của 344 là: 0b101011000 trong nhị phân. 0o530 trong bát phân.print[bin[dec], "in binary."] print[oct[dec], "in octal."] print[hex[dec], "in hexadecimal."] The decimal value of 344 is: 0b101011000 in binary. 0o530 in octal.

Làm thế nào để bạn đại diện cho Octal và thập lục phân trong Python?

Các takeaways rất đơn giản:..

Nhị phân sử dụng bin [] và '0b' ..

HexAdeCimal sử dụng hex [] và '0x' ..

Octal sử dụng OCT [] và '0O' ..

Hàm int [] có thể được sử dụng để chuyển đổi số thành số nguyên cơ sở 10 từ bất kỳ cơ sở nào từ 2 đến 36 bằng cách thay đổi tham số thứ hai. ví dụ. int [số, 30].

Hexadecimal thập phân nhị phân nhị phân là gì?

Một số octal với cơ sở 8 sử dụng tám chữ số 0,1,2,3,4,5,6 và 7. Một biểu diễn nhỏ gọn hơn được sử dụng bởi biểu diễn thập lục phân mà nhóm 4 chữ số nhị phân với nhau. Nó có thể sử dụng 16 chữ số, nhưng vì chúng ta chỉ có 10 chữ số, 6 chữ số còn lại được tạo thành từ 6 chữ cái đầu tiên của bảng chữ cái. A more compact representation is used by Hexadecimal representation which groups 4 binary digits together. It can make use of 16 digits, but since we have only 10 digits, the remaining 6 digits are made up of first 6 letters of the alphabet.

Bài Viết Liên Quan

Chủ Đề