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'
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]]
20xff
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
0xff
0xff
__index__ function called
0x64
class Data:
id = 0
def __index__[self]:
print['__index__ function called']
return self.id
d = Data[]
d.id = 100
print[hex[d]]
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]]
0hex[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[255]
'0xff'
>>> hex[-42]
'-0x2a'
>>> hex[1L]
'0x1L'
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
9class 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
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
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ả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
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
50xff
6>>> hex[255]
'0xff'
>>> hex[-42]
'-0x2a'
>>> hex[1L]
'0x1L'
80xff
80xff
9 0x1234567L
00xff
60x1234567L
2223Đầu ra: & nbsp;
0xff
0Biến thể đầu vào a] Demonstrate TypeError when floating point values are passed as parameter.
Python3
0xff
50xff
6print[hex[255]] # decimal
print[hex[0b111]] # binary
print[hex[0o77]] # octal
print[hex[0XFF]] # hexadecimal
9Biến thể đầu vào b]
Đầu ra: & nbsp;
0xff
1Biế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
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]]
7hex[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
hex[x] Parameters : x - an integer number [int object] Returns : Returns hexadecimal string.9
0xff
50xff
60xff
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
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 0xff
222____1010xff
240xff
030xff
0440x1234567L
3hex[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
31hex[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
60xff
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
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
90xff
440xff
010xff
460xff
030xff
040x1234567L
3Đầu ra: & nbsp;
Biến thể đầu vào a]
0xff
2Biến thể đầu vào b]
0xff
3