1
Mới! Lưu câu hỏi hoặc câu trả lời và sắp xếp nội dung yêu thích của bạn. Tìm hiểu thêm.
Learn more.
Tôi có một tập lệnh đơn giản chuyển lệnh [giá trị hex] cho UC bằng I2C. Tôi muốn chuyển các giá trị địa chỉ và lệnh thông qua dòng lệnh là Argv's.
Đây là mã của tôi:
import smbus
import time
from sys import argv
bus = smbus.SMBus[1]
addr = argv[1]
cmd = argv[2]
#address is 0x09
#commands = [0x16,0x06,0x17,0x07,0x18,0x08,0x19,0x09]
bus.write_byte[addr,cmd]
Tôi đã cố gắng viết
python progam.py 0x09 0x19
python program.py 9 25
Và cũng đã cố gắng chuyển đổi argv thành int [] sau đó thành hex. Không có những điều này đã làm việc.
Làm thế nào tôi có thể chuyển các giá trị hex vào chương trình của tôi?
Đã hỏi ngày 23 tháng 6 năm 2016 lúc 18:34Jun 23, 2016 at 18:34
4
import smbus
import time
from sys import argv
bus = smbus.SMBus[1]
if sys.argv[1].startswith["0x"]: # base 16
addr = int[argv[1][2:],16]
cmd = int[argv[2][2:],16]
else: # base 10
addr = int[argv[1]]
cmd = int[argv[2]]
print [addr,cmd] # you should see no quotes indicating that these are indeed ints now
bus.write_byte[addr,cmd]
Sau đó gọi nó bằng $ python my_script.py 9 25
hoặc gọi nó bằng
python progam.py 0x09 0x19
python program.py 9 25
0Đã trả lời ngày 23 tháng 6 năm 2016 lúc 18:40Jun 23, 2016 at 18:40
Joran Beasleyjoran BeasleyJoran Beasley
106K12 Huy hiệu vàng149 Huy hiệu bạc174 Huy hiệu đồng12 gold badges149 silver badges174 bronze badges
6
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
python progam.py 0x09 0x19
python program.py 9 25
1python progam.py 0x09 0x19
python program.py 9 25
2python progam.py 0x09 0x19
python program.py 9 25
3Lỗi và ngoại lệ: & nbsp;
python progam.py 0x09 0x19
python program.py 9 25
1python progam.py 0x09 0x19
python program.py 9 25
2import smbus
import time
from sys import argv
bus = smbus.SMBus[1]
if sys.argv[1].startswith["0x"]: # base 16
addr = int[argv[1][2:],16]
cmd = int[argv[2][2:],16]
else: # base 10
addr = int[argv[1]]
cmd = int[argv[2]]
print [addr,cmd] # you should see no quotes indicating that these are indeed ints now
bus.write_byte[addr,cmd]
2& nbsp; & nbsp; mã số 1: Minh họa việc sử dụng hàm hex []. & nbsp;
python progam.py 0x09 0x19
python program.py 9 25
4python progam.py 0x09 0x19
python program.py 9 25
5 python progam.py 0x09 0x19
python program.py 9 25
6python progam.py 0x09 0x19
python program.py 9 25
2python progam.py 0x09 0x19
python program.py 9 25
8python progam.py 0x09 0x19
python program.py 9 25
9import smbus
import time
from sys import argv
bus = smbus.SMBus[1]
if sys.argv[1].startswith["0x"]: # base 16
addr = int[argv[1][2:],16]
cmd = int[argv[2][2:],16]
else: # base 10
addr = int[argv[1]]
cmd = int[argv[2]]
print [addr,cmd] # you should see no quotes indicating that these are indeed ints now
bus.write_byte[addr,cmd]
3import smbus
import time
from sys import argv
bus = smbus.SMBus[1]
if sys.argv[1].startswith["0x"]: # base 16
addr = int[argv[1][2:],16]
cmd = int[argv[2][2:],16]
else: # base 10
addr = int[argv[1]]
cmd = int[argv[2]]
print [addr,cmd] # you should see no quotes indicating that these are indeed ints now
bus.write_byte[addr,cmd]
4 python progam.py 0x09 0x19
python program.py 9 25
5 python progam.py 0x09 0x19
python program.py 9 25
6__12Đầu ra: & nbsp;
The hexadecimal form of 23 is 0x17 The hexadecimal form of the ascii value os 'a' is 0x61 The hexadecimal form of 3.9 is 0x1.f333333333333p+1
Biến thể đầu vào a] Demonstrate TypeError when floating point values are passed as parameter.
Python3
python progam.py 0x09 0x19
python program.py 9 25
1python progam.py 0x09 0x19
python program.py 9 25
2TypeError : Returns TypeError when anything other than integer type constants are passed as parameters.5
Biến thể đầu vào b]
Đầu ra: & nbsp;
Traceback [most recent call last]: File "/home/7e1ac7e34362fd690cdb72cf294502e1.py", line 2, in print["The hexadecimal form of 11.1 is "+hex[11.1]] TypeError: 'float' object cannot be interpreted as an integer
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
Làm thế nào để bạn gán một giá trị hex cho một biến trong Python?
python progam.py 0x09 0x19
python program.py 9 25
1python progam.py 0x09 0x19
python program.py 9 25
2Traceback [most recent call last]: File "/home/7e1ac7e34362fd690cdb72cf294502e1.py", line 2, in print["The hexadecimal form of 11.1 is "+hex[11.1]] TypeError: 'float' object cannot be interpreted as an integer2
Traceback [most recent call last]: File "/home/7e1ac7e34362fd690cdb72cf294502e1.py", line 2, in print["The hexadecimal form of 11.1 is "+hex[11.1]] TypeError: 'float' object cannot be interpreted as an integer3
python progam.py 0x09 0x19
python program.py 9 25
1python progam.py 0x09 0x19
python program.py 9 25
2Traceback [most recent call last]: File "/home/7e1ac7e34362fd690cdb72cf294502e1.py", line 2, in print["The hexadecimal form of 11.1 is "+hex[11.1]] TypeError: 'float' object cannot be interpreted as an integer6
Traceback [most recent call last]: File "/home/7e1ac7e34362fd690cdb72cf294502e1.py", line 2, in print["The hexadecimal form of 11.1 is "+hex[11.1]] TypeError: 'float' object cannot be interpreted as an integer3
python progam.py 0x09 0x19
python program.py 9 25
1python progam.py 0x09 0x19
python program.py 9 25
2Enter a number with base 10 123 a. Decimal to Hexadecimal b. Decimal to Octal c. Decimal to Binary Enter your choice:- a Hexadecimal form of 123 is 7b0
Traceback [most recent call last]: File "/home/7e1ac7e34362fd690cdb72cf294502e1.py", line 2, in print["The hexadecimal form of 11.1 is "+hex[11.1]] TypeError: 'float' object cannot be interpreted as an integer3
python progam.py 0x09 0x19
python program.py 9 25
1python progam.py 0x09 0x19
python program.py 9 25
2Enter a number with base 10 123 a. Decimal to Hexadecimal b. Decimal to Octal c. Decimal to Binary Enter your choice:- a Hexadecimal form of 123 is 7b4
Traceback [most recent call last]: File "/home/7e1ac7e34362fd690cdb72cf294502e1.py", line 2, in print["The hexadecimal form of 11.1 is "+hex[11.1]] TypeError: 'float' object cannot be interpreted as an integer3
Enter a number with base 10 123 a. Decimal to Hexadecimal b. Decimal to Octal c. Decimal to Binary Enter your choice:- a Hexadecimal form of 123 is 7b6
The hexadecimal form of 23 is 0x17 The hexadecimal form of the ascii value os 'a' is 0x61 The hexadecimal form of 3.9 is 0x1.f333333333333p+13
The hexadecimal form of 23 is 0x17 The hexadecimal form of the ascii value os 'a' is 0x61 The hexadecimal form of 3.9 is 0x1.f333333333333p+16
Enter a number with base 10 123 a. Decimal to Hexadecimal b. Decimal to Octal c. Decimal to Binary Enter your choice:- a Hexadecimal form of 123 is 7b9
Enter a number with base 10 123456789 a. Decimal to Hexadecimal b. Decimal to Octal c. Decimal to Binary Enter your choice:- a Hexadecimal form of 123456789 is 75bcd150
Enter a number with base 10 123 a. Decimal to Hexadecimal b. Decimal to Octal c. Decimal to Binary Enter your choice:- a Hexadecimal form of 123 is 7b6
Enter a number with base 10 123456789 a. Decimal to Hexadecimal b. Decimal to Octal c. Decimal to Binary Enter your choice:- a Hexadecimal form of 123456789 is 75bcd152
hex[x] Parameters : x - an integer number [int object] Returns : Returns hexadecimal string.0
Enter a number with base 10 123456789 a. Decimal to Hexadecimal b. Decimal to Octal c. Decimal to Binary Enter your choice:- a Hexadecimal form of 123456789 is 75bcd154
Enter a number with base 10 123456789 a. Decimal to Hexadecimal b. Decimal to Octal c. Decimal to Binary Enter your choice:- a Hexadecimal form of 123456789 is 75bcd155
python progam.py 0x09 0x19
python program.py 9 25
1__12$ python my_script.py 9 25
3$ python my_script.py 9 25
4
python progam.py 0x09 0x19
python program.py 9 25
5 python progam.py 0x09 0x19
python program.py 9 25
6$ python my_script.py 9 25
7Enter a number with base 10 123456789 a. Decimal to Hexadecimal b. Decimal to Octal c. Decimal to Binary Enter your choice:- a Hexadecimal form of 123456789 is 75bcd15
$ python my_script.py 9 25
9python progam.py 0x09 0x19
python program.py 9 25
00python progam.py 0x09 0x19
python program.py 9 25
9Enter a number with base 10 123456789 a. Decimal to Hexadecimal b. Decimal to Octal c. Decimal to Binary Enter your choice:- a Hexadecimal form of 123456789 is 75bcd150
Enter a number with base 10 123 a. Decimal to Hexadecimal b. Decimal to Octal c. Decimal to Binary Enter your choice:- a Hexadecimal form of 123 is 7b6
Enter a number with base 10 123456789 a. Decimal to Hexadecimal b. Decimal to Octal c. Decimal to Binary Enter your choice:- a Hexadecimal form of 123456789 is 75bcd152
python progam.py 0x09 0x19
python program.py 9 25
05Enter a number with base 10 123456789 a. Decimal to Hexadecimal b. Decimal to Octal c. Decimal to Binary Enter your choice:- a Hexadecimal form of 123456789 is 75bcd154
Enter a number with base 10 123456789 a. Decimal to Hexadecimal b. Decimal to Octal c. Decimal to Binary Enter your choice:- a Hexadecimal form of 123456789 is 75bcd155
python progam.py 0x09 0x19
python program.py 9 25
1python progam.py 0x09 0x19
python program.py 9 25
2$ python my_script.py 9 25
3$ python my_script.py 9 25
4
python progam.py 0x09 0x19
python program.py 9 25
5 python progam.py 0x09 0x19
python program.py 9 25
18$ python my_script.py 9 25
7python progam.py 0x09 0x19
python program.py 9 25
20$ python my_script.py 9 25
9____100python progam.py 0x09 0x19
python program.py 9 25
9Enter a number with base 10 123456789 a. Decimal to Hexadecimal b. Decimal to Octal c. Decimal to Binary Enter your choice:- a Hexadecimal form of 123456789 is 75bcd150
Enter a number with base 10 123 a. Decimal to Hexadecimal b. Decimal to Octal c. Decimal to Binary Enter your choice:- a Hexadecimal form of 123 is 7b6
Enter a number with base 10 123456789 a. Decimal to Hexadecimal b. Decimal to Octal c. Decimal to Binary Enter your choice:- a Hexadecimal form of 123456789 is 75bcd152
python progam.py 0x09 0x19
python program.py 9 25
27Enter a number with base 10 123456789 a. Decimal to Hexadecimal b. Decimal to Octal c. Decimal to Binary Enter your choice:- a Hexadecimal form of 123456789 is 75bcd154
Enter a number with base 10 123456789 a. Decimal to Hexadecimal b. Decimal to Octal c. Decimal to Binary Enter your choice:- a Hexadecimal form of 123456789 is 75bcd155
python progam.py 0x09 0x19
python program.py 9 25
1__12$ python my_script.py 9 25
3$ python my_script.py 9 25
4
python progam.py 0x09 0x19
python program.py 9 25
5python progam.py 0x09 0x19
python program.py 9 25
40$ python my_script.py 9 25
7python progam.py 0x09 0x19
python program.py 9 25
42$ python my_script.py 9 25
9python progam.py 0x09 0x19
python program.py 9 25
00python progam.py 0x09 0x19
python program.py 9 25
9Đầu ra: & nbsp;
Biến thể đầu vào a]
Enter a number with base 10 123 a. Decimal to Hexadecimal b. Decimal to Octal c. Decimal to Binary Enter your choice:- a Hexadecimal form of 123 is 7b
Biến thể đầu vào b]
Enter a number with base 10 123456789 a. Decimal to Hexadecimal b. Decimal to Octal c. Decimal to Binary Enter your choice:- a Hexadecimal form of 123456789 is 75bcd15