Hướng dẫn how do you pass a hex value in python? - làm thế nào để bạn chuyển một giá trị hex trong python?

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

Hướng dẫn how do you pass a hex value in python? - làm thế nào để bạn chuyển một giá trị hex trong python?

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

  • Đọ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

    python progam.py 0x09 0x19
    python program.py 9 25
    
    1
    python progam.py 0x09 0x19
    python program.py 9 25
    
    2
    python progam.py 0x09 0x19
    python program.py 9 25
    
    3

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

    python progam.py 0x09 0x19
    python program.py 9 25
    
    1
    python progam.py 0x09 0x19
    python program.py 9 25
    
    2
    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)
    
    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
    
    4
    python progam.py 0x09 0x19
    python program.py 9 25
    
    5
    python progam.py 0x09 0x19
    python program.py 9 25
    
    6
    python progam.py 0x09 0x19
    python program.py 9 25
    
    2
    python progam.py 0x09 0x19
    python program.py 9 25
    
    8
    python progam.py 0x09 0x19
    python program.py 9 25
    
    9

    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)
    
    3
    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)
    
    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
    
    1
    python progam.py 0x09 0x19
    python program.py 9 25
    
    2
    TypeError :  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
    
    1
    python progam.py 0x09 0x19
    python program.py 9 25
    
    2
    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
    2
    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
    3

    python progam.py 0x09 0x19
    python program.py 9 25
    
    1
    python progam.py 0x09 0x19
    python program.py 9 25
    
    2
    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
    6
    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
    3

    python progam.py 0x09 0x19
    python program.py 9 25
    
    1
    python progam.py 0x09 0x19
    python program.py 9 25
    
    2
    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
    0
    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
    3

    python progam.py 0x09 0x19
    python program.py 9 25
    
    1
    python progam.py 0x09 0x19
    python program.py 9 25
    
    2
    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
    4
    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
    3

    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
    6
    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
    3
    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
    6
    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
    9

    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
    0
    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
    6
    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
    2
    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 75bcd15
    4

    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
    5
    python progam.py 0x09 0x19
    python program.py 9 25
    
    1__12

    $ python my_script.py 9 253$ python my_script.py 9 254

    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 257
    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
    $ python my_script.py 9 259
    python progam.py 0x09 0x19
    python program.py 9 25
    
    00
    python progam.py 0x09 0x19
    python program.py 9 25
    
    9

    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
    0
    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
    6
    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
    2
    python progam.py 0x09 0x19
    python program.py 9 25
    
    05
    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
    4

    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
    5
    python progam.py 0x09 0x19
    python program.py 9 25
    
    1
    python progam.py 0x09 0x19
    python program.py 9 25
    
    2

    $ python my_script.py 9 253$ python my_script.py 9 254

    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 257
    python progam.py 0x09 0x19
    python program.py 9 25
    
    20$ python my_script.py 9 259____100
    python progam.py 0x09 0x19
    python program.py 9 25
    
    9

    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
    0
    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
    6
    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
    2
    python progam.py 0x09 0x19
    python program.py 9 25
    
    27
    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
    4

    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
    5
    python progam.py 0x09 0x19
    python program.py 9 25
    
    1__12

    $ python my_script.py 9 253$ python my_script.py 9 254

    python progam.py 0x09 0x19
    python program.py 9 25
    
    5
    python progam.py 0x09 0x19
    python program.py 9 25
    
    40$ python my_script.py 9 257
    python progam.py 0x09 0x19
    python program.py 9 25
    
    42$ python my_script.py 9 259
    python progam.py 0x09 0x19
    python program.py 9 25
    
    00
    python 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

    Làm thế nào để bạn gán một giá trị hex cho một biến 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.

    Làm thế nào để bạn tuyên bố một biến hex?

    Để gán giá trị theo định dạng thập lục phân cho một biến, chúng tôi sử dụng hậu tố 0x hoặc 0x. Nó nói với trình biên dịch rằng giá trị (được hậu tố với 0x hoặc 0x) là giá trị thập lục phân và gán nó cho biến.use 0x or 0X suffix. It tells to the compiler that the value (suffixed with 0x or 0X) is a hexadecimal value and assigns it to the variable.

    Làm thế nào để bạn tăng giá trị hex trong python?

    FYI Giá trị thập phân 1 bằng với giá trị HEX 0x01. Ví dụ: nói 15 + 1 = 16, giống hệt với 0x0F + 0x01 = 0x10. Vì vậy, bạn có thể tăng theo nghĩa đen bất kỳ cơ sở nào bằng cách thêm 1.adding 1.

    Làm cách nào để in một giá trị hex mà không có 0x trong Python?

    Để in một thập lục phân tích dương hoặc âm mà không có tiền tố '0x' hoặc '-0x', bạn chỉ cần sử dụng phương thức String.replace ('x', '0') và thay thế từng lần xuất hiện 'X' bằng '0'.Chuỗi kết quả là chính xác về mặt toán học vì dẫn đầu không thay đổi giá trị của số.use the string. replace('x', '0') method and replace each occurrence of 'x' with '0' . The resulting string is mathematically correct because leading '0' s don't change the value of the number.

    Làm cách nào để chuyển đổi chuỗi hex thành giá trị số nguyên?

    Để chuyển đổi một chuỗi thập lục phân thành một số..
    Sử dụng phương thức TOINT32 (Chuỗi, Int32) để chuyển đổi số được biểu thị trong cơ sở-16 thành số nguyên.Đối số đầu tiên của phương thức toint32 (chuỗi, int32) là chuỗi để chuyển đổi.....
    Lưu ý rằng chuỗi thập lục phân có các hạn chế sau: Nó không thể bao gồm tiền tố & h ..