Code: Select all
def test():
x = b'\x02abcdefg\x03'
print("--------------------------------------------")
print(f"x: {x}")
print("--------------------------------------------")
print(f"type(x): {type(x)}")
print(f"len(x: {len(x)}")
print(f"x[0]: {x[0]}")
print(f"x[0] == 0x02: {x[0] == 0x02}")
print(f"type(x[0]): {type(x[0])}")
print(f"x[0] == 2: {x[0] == 2}")
n1 = b'a'
print(f"x.index('a'): {x.index(n1)}")
n2 = b'\x02'
print(f"x.index(b'\\0x02'): {x.index(n2)}")
# These four lines will throw exceptions on MicroPython
# They run without problems on Python 3.9
print(f"x.index(ord('a')): {x.index(ord('a'))}")
print(f"x.index(97): {x.index(97)}")
print(f"x.index(0x02): {x.index(0x02)}")
print(f"x.index(2): {x.index(2)}")
test()
Code: Select all
--------------------------------------------
x: b'\x02abcdefg\x03'
--------------------------------------------
type(x): <class 'bytes'>
len(x: 9
x[0]: 2
x[0] == 0x02: True
type(x[0]): <class 'int'>
x[0] == 2: True
x.index('a'): 1
x.index(b'\0x02'): 0
x.index(ord('a')): 1
x.index(97): 1
x.index(0x02): 0
x.index(2): 0
Process finished with exit code 0
Code: Select all
--------------------------------------------
x: b'\x02abcdefg\x03'
--------------------------------------------
type(x): <class 'bytes'>
len(x: 9
x[0]: 2
x[0] == 0x02: True
type(x[0]): <class 'int'>
x[0] == 2: True
x.index('a'): 1
x.index(b'\0x02'): 0
Traceback (most recent call last):
File "<stdin>", line 644, in <module>
File "<stdin>", line 639, in test
TypeError: can't convert 'int' object to str implicitly