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
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
50xff
60xff
7Lỗi và ngoại lệ: & nbsp;
0xff
50xff
60x1234567L
6& nbsp; & nbsp; mã số 1: Minh họa việc sử dụng hàm hex []. & nbsp;
0xff
80xff
9 0x1234567L
00xff
60x1234567L
22230x1234567L
70x1234567L
8 0xff
9 0x1234567L
00xff
6>>> hex[255]
'0xff'
>>> hex[-42]
'-0x2a'
>>> hex[1L]
'0x1L'
20xff
6>>> hex[255]
'0xff'
>>> hex[-42]
'-0x2a'
>>> hex[1L]
'0x1L'
4>>> hex[255]
'0xff'
>>> hex[-42]
'-0x2a'
>>> hex[1L]
'0x1L'
5
50xff
60xff
8 >>> hex[255]
'0xff'
>>> hex[-42]
'-0x2a'
>>> hex[1L]
'0x1L'
0xff
0
9>>> hex[255]
'0xff'
>>> hex[-42]
'-0x2a'
>>> hex[1L]
'0x1L'
9 0xff
1print[hex[255]] # decimal
print[hex[0b111]] # binary
print[hex[0o77]] # octal
print[hex[0XFF]] # hexadecimal
2print[hex[255]] # decimal
print[hex[0b111]] # binary
print[hex[0o77]] # octal
print[hex[0XFF]] # hexadecimal
00x1234567L
60xff
5print[hex[255]] # decimal
print[hex[0b111]] # binary
print[hex[0o77]] # octal
print[hex[0XFF]] # hexadecimal
3 Demonstrate TypeError when floating point values are passed as parameter. 0x1234567L
Python3
0xff
50xff
6print[hex[255]] # decimal
print[hex[0b111]] # binary
print[hex[0o77]] # octal
print[hex[0XFF]] # hexadecimal
9Đầu ra: & nbsp;
50xff
60xff
8 >>> hex[255]
'0xff'
>>> hex[-42]
'-0x2a'
>>> hex[1L]
'0x1L'
0xff
1>>> hex[255]
'0xff'
>>> hex[-42]
'-0x2a'
>>> hex[1L]
'0x1L'
90xff
9 print[hex[255]] # decimal
print[hex[0b111]] # binary
print[hex[0o77]] # octal
print[hex[0XFF]] # hexadecimal
1print[hex[255]] # decimal
print[hex[0b111]] # binary
print[hex[0o77]] # octal
print[hex[0XFF]] # hexadecimal
20x1234567L
00xff
6print[hex[255]] # decimal
print[hex[0b111]] # binary
print[hex[0o77]] # octal
print[hex[0XFF]] # hexadecimal
50x1234567L
3Applications :
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
50xff
6class Data:
id = 0
def __index__[self]:
print['__index__ function called']
return self.id
d = Data[]
d.id = 100
print[hex[d]]
6class Data:
id = 0
def __index__[self]:
print['__index__ function called']
return self.id
d = Data[]
d.id = 100
print[hex[d]]
70xff
50xff
6__index__ function called
0x64
0class Data:
id = 0
def __index__[self]:
print['__index__ function called']
return self.id
d = Data[]
d.id = 100
print[hex[d]]
70xff
50xff
6__index__ function called
0x64
4class Data:
id = 0
def __index__[self]:
print['__index__ function called']
return self.id
d = Data[]
d.id = 100
print[hex[d]]
70xff
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]]
70xff
80xff
9 0x1234567L
00xff
60xff
0x7
0x3f
0xff
40x1234567L
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
60xff
0x7
0x3f
0xff
7 0xff
0x7
0x3f
0xff
80xff
6class Data:
id = 0
def __index__[self]:
print['__index__ function called']
return self.id
d = Data[]
d.id = 100
print[hex[d]]
00xff
6class Data:
id = 0
def __index__[self]:
print['__index__ function called']
return self.id
d = Data[]
d.id = 100
print[hex[d]]
2hex[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]]
0hex[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'
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
50xff
6TypeError : 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
9TypeError : 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
00xff
010xff
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
09hex[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
90xff
440xff
010xff
460xff
030xff
040x1234567L
3
50xff
60xff
8 >>> hex[255]
'0xff'
>>> hex[-42]
'-0x2a'
>>> hex[1L]
'0x1L'
>>> hex[255]
'0xff'
>>> hex[-42]
'-0x2a'
>>> hex[1L]
'0x1L'
90xff
9 print[hex[255]] # decimal
print[hex[0b111]] # binary
print[hex[0o77]] # octal
print[hex[0XFF]] # hexadecimal
1print[hex[255]] # decimal
print[hex[0b111]] # binary
print[hex[0o77]] # octal
print[hex[0XFF]] # hexadecimal
20x1234567L
00xff
6print[hex[255]] # decimal
print[hex[0b111]] # binary
print[hex[0o77]] # octal
print[hex[0XFF]] # hexadecimal
50x1234567L
30xff
2Đầu ra: & nbsp;
0xff
3