Hướng dẫn how do you declare a hex string in python? - làm thế nào để bạn khai báo một chuỗi hex trong python?

Tôi không biết cách 'Pythonic' thích hợp để làm điều này nhưng đây là những gì tôi nghĩ ra.

def hex_string_to_bin_string[input]:
   lookup = {"0" : "0000", "1" : "0001", "2" : "0010", "3" : "0011", "4" : "0100", "5" : "0101", "6" : "0110", "7" : "0111", "8" : "1000", "9" : "1001", "A" : "1010", "B" : "1011", "C" : "1100", "D" : "1101", "E" : "1110", "F" : "1111"}
   result = ""
   for byte in input:
      result =  result + lookup[byte]
   return result
def hex_string_to_hex_value[input]:
   value = hex_string_to_bin_string[input]
   highest_order = len[value] - 1
   result = 0
   for bit in value:
      result = result + int[bit] * pow[2,highest_order]
      highest_order = highest_order - 1
   return hex[result]
print hex_string_to_hex_value["FF"]

Kết quả là

0xff

Cũng thử in hex_string_to_hex_value ["01234567"]

kết quả trong

0x1234567L

Lưu ý: L chỉ ra giá trị rơi vào danh mục "dài" theo như tôi có thể nói dựa trên tài liệu từ trang web Python [họ cho thấy rằng bạn có thể có một giá trị L được thêm vào]. //docs.python.org/2/l Library/functions.html#hex

>>> hex[255]
'0xff'
>>> hex[-42]
'-0x2a'
>>> hex[1L]
'0x1L'

Chức năng Python hex [] được sử dụng để chuyển đổi một số nguyên thành chuỗi thập lục phân chữ thường được đặt trước với số 0x 0x. Chúng ta cũng có thể chuyển một đối tượng cho hàm hex [], trong trường hợp đó, đối tượng phải có hàm

0xff
4 được xác định trả về số nguyên. Đối số số nguyên đầu vào có thể nằm trong bất kỳ cơ sở nào như nhị phân, bát phân, v.v. Python sẽ chăm sóc chúng chuyển đổi chúng thành định dạng thập lục phân.

Ví dụ về Python Hex []

Hãy cùng xem xét một số ví dụ đơn giản về việc chuyển đổi số nguyên thành số thập lục phân.

print[hex[255]]  # decimal
print[hex[0b111]]  # binary
print[hex[0o77]]  # octal
print[hex[0XFF]]  # hexadecimal

Output:

0xff
0x7
0x3f
0xff

Python hex [] với đối tượng

Hãy để tạo ra một lớp tùy chỉnh và xác định hàm __index __ [] để chúng ta có thể sử dụng hàm hex [] với nó.

class Data:
    id = 0

    def __index__[self]:
        print['__index__ function called']
        return self.id


d = Data[]
d.id = 100

print[hex[d]]

Output:

__index__ function called
0x64

Bạn có thể kiểm tra toàn bộ tập lệnh Python và nhiều ví dụ về Python từ Kho lưu trữ GitHub của chúng tôi.

Tham khảo: Tài liệu chính thức

Muốn tìm hiểu thêm? Tham gia cộng đồng DigitalOcean!

Tham gia cộng đồng DigitalOcean của chúng tôi miễn phí hơn một triệu nhà phát triển! Nhận trợ giúp và chia sẻ kiến ​​thức trong phần Câu hỏi & Câu trả lời của chúng tôi, tìm hướng dẫn và công cụ sẽ giúp bạn phát triển như một nhà phát triển và mở rộng quy mô dự án hoặc doanh nghiệp của bạn, và đăng ký các chủ đề quan tâm.

Đăng ký

Xem thảo luận

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • Xem thảo luận

    Cải thiện bài viết

    Lưu bài viết

    Đọc function is one of the built-in functions in Python3, which is used to convert an integer number into it’s corresponding hexadecimal form.

    Bàn luận 

    hex[x] 
    Parameters : 
    x - an integer number [int object]
    Returns :  Returns hexadecimal string.

    Chức năng hex [] là một trong những hàm tích hợp trong python3, được sử dụng để chuyển đổi số nguyên thành dạng hexadecimal tương ứng.

    TypeError :  Returns TypeError when anything other than
                 integer type constants are passed as parameters.

    Cú pháp: & nbsp;
    Code #1 : Illustrates use of hex[] function. 

    Python3

    0xff
    
    5
    0xff
    
    6
    0xff
    
    7

    Lỗi và ngoại lệ: & nbsp;

    0xff
    
    5
    0xff
    
    6
    0x1234567L
    
    6

    & nbsp; & nbsp; mã số 1: Minh họa việc sử dụng hàm hex []. & nbsp;

    0xff
    
    8
    0xff
    
    9
    0x1234567L
    
    0
    0xff
    
    6
    0x1234567L
    
    2223

    0x1234567L
    
    7
    0x1234567L
    
    8
    0xff
    
    9
    0x1234567L
    
    0
    0xff
    
    6
    >>> hex[255]
    '0xff'
    >>> hex[-42]
    '-0x2a'
    >>> hex[1L]
    '0x1L'
    
    2
    0xff
    
    6
    >>> hex[255]
    '0xff'
    >>> hex[-42]
    '-0x2a'
    >>> hex[1L]
    '0x1L'
    
    4
    >>> hex[255]
    '0xff'
    >>> hex[-42]
    '-0x2a'
    >>> hex[1L]
    '0x1L'
    
    5

    0xff
    
    5
    0xff
    
    6
    >>> hex[255]
    '0xff'
    >>> hex[-42]
    '-0x2a'
    >>> hex[1L]
    '0x1L'
    
    8
     

    0xff
    
    0

    >>> hex[255]
    '0xff'
    >>> hex[-42]
    '-0x2a'
    >>> hex[1L]
    '0x1L'
    
    9
    0xff
    
    9
    print[hex[255]]  # decimal
    print[hex[0b111]]  # binary
    print[hex[0o77]]  # octal
    print[hex[0XFF]]  # hexadecimal
    
    1
    print[hex[255]]  # decimal
    print[hex[0b111]]  # binary
    print[hex[0o77]]  # octal
    print[hex[0XFF]]  # hexadecimal
    
    2
    0x1234567L
    
    0
    0xff
    
    6
    print[hex[255]]  # decimal
    print[hex[0b111]]  # binary
    print[hex[0o77]]  # octal
    print[hex[0XFF]]  # hexadecimal
    
    5
    0x1234567L
    
    3
    Demonstrate TypeError when floating point values are passed as parameter. 

    Python3

    0xff
    
    5
    0xff
    
    6
    print[hex[255]]  # decimal
    print[hex[0b111]]  # binary
    print[hex[0o77]]  # octal
    print[hex[0XFF]]  # hexadecimal
    
    9

    Đầu ra: & nbsp;

    0xff
    
    5
    0xff
    
    6
    >>> hex[255]
    '0xff'
    >>> hex[-42]
    '-0x2a'
    >>> hex[1L]
    '0x1L'
    
    8
     

    0xff
    
    1

    >>> hex[255]
    '0xff'
    >>> hex[-42]
    '-0x2a'
    >>> hex[1L]
    '0x1L'
    
    9
    0xff
    
    9
    print[hex[255]]  # decimal
    print[hex[0b111]]  # binary
    print[hex[0o77]]  # octal
    print[hex[0XFF]]  # hexadecimal
    
    1
    print[hex[255]]  # decimal
    print[hex[0b111]]  # binary
    print[hex[0o77]]  # octal
    print[hex[0XFF]]  # hexadecimal
    
    2
    0x1234567L
    
    0
    0xff
    
    6
    print[hex[255]]  # decimal
    print[hex[0b111]]  # binary
    print[hex[0o77]]  # octal
    print[hex[0XFF]]  # hexadecimal
    
    5
    0x1234567L
    
    3
    Applications : 
    hex[] is used in all the standard conversions. For example conversion of hexadecimal to decimal, hexadecimal to octal, hexadecimal to binary.
     

    Đầu ra: & nbsp; 

    Python3

    Mã số 2: Chứng minh kiểu mẫu khi các giá trị điểm nổi được truyền dưới dạng tham số. & NBSP;

    0xff
    
    5
    0xff
    
    6
    class Data:
        id = 0
    
        def __index__[self]:
            print['__index__ function called']
            return self.id
    
    
    d = Data[]
    d.id = 100
    
    print[hex[d]]
    
    6
    class Data:
        id = 0
    
        def __index__[self]:
            print['__index__ function called']
            return self.id
    
    
    d = Data[]
    d.id = 100
    
    print[hex[d]]
    
    7

    0xff
    
    5
    0xff
    
    6
    __index__ function called
    0x64
    
    0
    class Data:
        id = 0
    
        def __index__[self]:
            print['__index__ function called']
            return self.id
    
    
    d = Data[]
    d.id = 100
    
    print[hex[d]]
    
    7

    0xff
    
    5
    0xff
    
    6
    __index__ function called
    0x64
    
    4
    class Data:
        id = 0
    
        def __index__[self]:
            print['__index__ function called']
            return self.id
    
    
    d = Data[]
    d.id = 100
    
    print[hex[d]]
    
    7

    0xff
    
    5
    0xff
    
    6
    __index__ function called
    0x64
    
    8
    class Data:
        id = 0
    
        def __index__[self]:
            print['__index__ function called']
            return self.id
    
    
    d = Data[]
    d.id = 100
    
    print[hex[d]]
    
    7

    0xff
    
    8
    0xff
    
    9
    0x1234567L
    
    0
    0xff
    
    6
    0xff
    0x7
    0x3f
    0xff
    
    4
    0x1234567L
    
    3

    & nbsp; & nbsp; Ứng dụng: & nbsp; hex [] được sử dụng trong tất cả các chuyển đổi tiêu chuẩn. Ví dụ, chuyển đổi thập lục phân sang thập phân, thập lục phân sang bát phân, thập lục phân sang nhị phân. & Nbsp;

    Mã số 3: & NBSP;

    0xff
    0x7
    0x3f
    0xff
    
    6
    0xff
    0x7
    0x3f
    0xff
    
    7
    0xff
    0x7
    0x3f
    0xff
    
    8
    0xff
    
    6
    class Data:
        id = 0
    
        def __index__[self]:
            print['__index__ function called']
            return self.id
    
    
    d = Data[]
    d.id = 100
    
    print[hex[d]]
    
    0
    0xff
    
    6
    class Data:
        id = 0
    
        def __index__[self]:
            print['__index__ function called']
            return self.id
    
    
    d = Data[]
    d.id = 100
    
    print[hex[d]]
    
    2

    hex[x] 
    Parameters : 
    x - an integer number [int object]
    Returns :  Returns hexadecimal string.
    0
    0xff
    0x7
    0x3f
    0xff
    
    7
    class Data:
        id = 0
    
        def __index__[self]:
            print['__index__ function called']
            return self.id
    
    
    d = Data[]
    d.id = 100
    
    print[hex[d]]
    
    0
    hex[x] 
    Parameters : 
    x - an integer number [int object]
    Returns :  Returns hexadecimal string.
    3

    hex[x] 
    Parameters : 
    x - an integer number [int object]
    Returns :  Returns hexadecimal string.
    4
    hex[x] 
    Parameters : 
    x - an integer number [int object]
    Returns :  Returns hexadecimal string.
    0
    hex[x] 
    Parameters : 
    x - an integer number [int object]
    Returns :  Returns hexadecimal string.
    6
    >>> hex[255]
    '0xff'
    >>> hex[-42]
    '-0x2a'
    >>> hex[1L]
    '0x1L'
    
    4
    hex[x] 
    Parameters : 
    x - an integer number [int object]
    Returns :  Returns hexadecimal string.
    8

    hex[x] 
    Parameters : 
    x - an integer number [int object]
    Returns :  Returns hexadecimal string.
    9
    0xff
    
    5
    0xff
    
    6
    TypeError :  Returns TypeError when anything other than
                 integer type constants are passed as parameters.
    2
    0xff
    
    9
    TypeError :  Returns TypeError when anything other than
                 integer type constants are passed as parameters.
    4
    TypeError :  Returns TypeError when anything other than
                 integer type constants are passed as parameters.
    5
    0xff
    
    9

    TypeError :  Returns TypeError when anything other than
                 integer type constants are passed as parameters.
    7
    TypeError :  Returns TypeError when anything other than
                 integer type constants are passed as parameters.
    8
    0xff
    
    9
    0x1234567L
    
    0
    0xff
    
    01
    0xff
    
    022.

    hex[x] 
    Parameters : 
    x - an integer number [int object]
    Returns :  Returns hexadecimal string.
    4
    hex[x] 
    Parameters : 
    x - an integer number [int object]
    Returns :  Returns hexadecimal string.
    0
    hex[x] 
    Parameters : 
    x - an integer number [int object]
    Returns :  Returns hexadecimal string.
    6
    0xff
    
    09
    hex[x] 
    Parameters : 
    x - an integer number [int object]
    Returns :  Returns hexadecimal string.
    8

    TypeError :  Returns TypeError when anything other than
                 integer type constants are passed as parameters.
    7
    TypeError :  Returns TypeError when anything other than
                 integer type constants are passed as parameters.
    8
    0xff
    
    9
    0xff
    
    44
    0xff
    
    01
    0xff
    
    46
    0xff
    
    03
    0xff
    
    04
    0x1234567L
    
    3

    0xff
    
    5
    0xff
    
    6
    >>> hex[255]
    '0xff'
    >>> hex[-42]
    '-0x2a'
    >>> hex[1L]
    '0x1L'
    
    8
     

    >>> hex[255]
    '0xff'
    >>> hex[-42]
    '-0x2a'
    >>> hex[1L]
    '0x1L'
    
    9
    0xff
    
    9
    print[hex[255]]  # decimal
    print[hex[0b111]]  # binary
    print[hex[0o77]]  # octal
    print[hex[0XFF]]  # hexadecimal
    
    1
    print[hex[255]]  # decimal
    print[hex[0b111]]  # binary
    print[hex[0o77]]  # octal
    print[hex[0XFF]]  # hexadecimal
    
    2
    0x1234567L
    
    0
    0xff
    
    6
    print[hex[255]]  # decimal
    print[hex[0b111]]  # binary
    print[hex[0o77]]  # octal
    print[hex[0XFF]]  # hexadecimal
    
    5
    0x1234567L
    
    3

    0xff
    
    2

    Đầu ra: & nbsp;

    0xff
    
    3

    Làm thế nào để bạn tuyên bố một hình lục giác trong Python?

    Khi biểu thị số thập lục phân trong Python, tiền tố các số có '0x'. Ngoài ra, sử dụng hàm hex [] để chuyển đổi các giá trị thành định dạng thập lục phân cho mục đích hiển thị.prefix the numbers with '0x'. Also, use the hex[] function to convert values to hexadecimal format for display purposes.

    Chuỗi hex trong Python là gì?

    Chức năng Python hex [] được sử dụng để chuyển đổi một số nguyên thành chuỗi thập lục phân chữ thường được đặt trước với số 0x 0x. Chúng ta cũng có thể chuyển một đối tượng cho hàm hex [], trong trường hợp đó, đối tượng phải có hàm __index __ [] được xác định trả về số nguyên. Đối số số nguyên đầu vào có thể nằm trong bất kỳ cơ sở nào như nhị phân, bát phân, v.v.used to convert an integer to a lowercase hexadecimal string prefixed with “0x”. We can also pass an object to hex[] function, in that case the object must have __index__[] function defined that returns integer. The input integer argument can be in any base such as binary, octal etc.

    Làm thế nào để bạn đọc một chuỗi hex trong python?

    Nếu bạn đang sử dụng trình thông dịch Python, bạn chỉ có thể nhập 0x [giá trị hex của bạn] và trình thông dịch sẽ tự động chuyển đổi nó cho bạn.type 0x[your hex value] and the interpreter will convert it automatically for you.

    Làm thế nào để bạn in hex trong python?

    Python: hàm hex []..
    Phiên bản: ... .
    Cú pháp: Hex [x].
    Tham số: ... .
    Ví dụ: Python hex [] hàm số = 127 in [số, 'bằng hex =', hex [số]] số = 0 in [số, 'bằng hex =', hex [số]] số = -35 in [số.

    Chủ Đề