Hướng dẫn how do you declare a hex variable in python? - làm thế nào để bạn khai báo một biến 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). https://docs.python.org/2/l Library/functions.html#hex

>>> hex(255)
'0xff'
>>> hex(-42)
'-0x2a'
>>> hex(1L)
'0x1L'

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

0xff 50xff 6__index__ function called 0x64 8class Data: id = 0 def __index__(self): print('__index__ function called') return self.id d = Data() d.id = 100 print(hex(d)) 7

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

print(hex(255))  # decimal
print(hex(0b111))  # binary
print(hex(0o77))  # octal
print(hex(0XFF))  # hexadecimal

Output:

0xff
0x7
0x3f
0xff

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.0hex(x) Parameters : x - an integer number (int object) Returns : Returns hexadecimal string.6 >>> hex(255) '0xff' >>> hex(-42) '-0x2a' >>> hex(1L) '0x1L' 4hex(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

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

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

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ý

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

Lưu bài viết

  • Đọc
  • Bàn 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 

    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.

    Cú pháp: & nbsp;

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

    ________số 8
    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
    
    5
    0xff
    
    6
    >>> hex(255)
    '0xff'
    >>> hex(-42)
    '-0x2a'
    >>> hex(1L)
    '0x1L'
    
    8

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

    Đầu ra: & nbsp; 

    0xff
    
    0

    Biến thể đầu vào a) 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

    Biến thể đầu vào b)

    Đầu ra: & nbsp; 

    0xff
    
    1

    Biến thể đầu vào a)
    Applications : 
    hex() is used in all the standard conversions. For example conversion of hexadecimal to decimal, hexadecimal to octal, hexadecimal to binary.
     

    Biến thể đầu vào b) 

    Python3

    Bạn có thể chuyển đổi chuỗi thành hex trong python không?

    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

    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

    hex(x) 
    Parameters : 
    x - an integer number (int object)
    Returns :  Returns hexadecimal string.
    9
    0xff
    
    5
    0xff
    
    6
    0xff
    
    14
    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
    0xff
    
    222____101
    0xff
    
    24
    0xff
    
    03
    0xff
    
    044
    0x1234567L
    
    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
    0xff
    
    31
    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
    0xff
    
    36
    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
    0xff
    
    44
    0xff
    
    01
    0xff
    
    46
    0xff
    
    03
    0xff
    
    04
    0x1234567L
    
    3

    Đầu ra: & nbsp; 

    Biến thể đầu vào a)

    0xff
    
    2

    Biến thể đầu vào b)

    0xff
    
    3

    Bạn có thể chuyển đổi chuỗi thành hex trong python không?

    Hàm hex () chuyển đổi một số số nguyên được chỉ định thành một biểu diễn chuỗi thập lục phân.Sử dụng int (x, cơ sở) với 16 làm cơ sở để chuyển đổi chuỗi X thành một số nguyên.Gọi hex (số) với số nguyên dưới dạng số để chuyển đổi nó thành thập lục phân.. Use int(x, base) with 16 as base to convert the string x to an integer. Call hex(number) with the integer as number to convert it to hexadecimal.

    Loại dữ liệu nào là hex?

    Các loại dữ liệu octal và thập lục phân là các loại số nguyên có sẵn trong hầu hết các ngôn ngữ máy tính.Họ cung cấp một ký hiệu thuận tiện cho việc xây dựng các giá trị số nguyên trong hệ thống số nhị phân.Tất cả các giá trị số nguyên được thể hiện trong bộ nhớ máy tính bằng cách đặt các giá trị của các chữ số nhị phân.integer types that are available in most computer languages. They provide a convenient notation for the construction of integer values in the binary number system. All integer values are expressed in computer memory by setting the values of binary digits.