Hướng dẫn python append bytes - python nối thêm byte

First of all passing an integer[say n] to

>>> bytes[chr[5], 'ascii']
b'\x05'
0 simply returns an bytes string of n length with null bytes. So, that's not what you want here:

Either you can do:

>>> bytes[[5]] #This will work only for range 0-256.
b'\x05'

Or:

>>> bytes[chr[5], 'ascii']
b'\x05'

As @simonzack already mentioned, bytes are immutable, so to update [or better say re-assign] its value, you need to use the

>>> bytes[chr[5], 'ascii']
b'\x05'
2 operator.

>>> s = b'\x01\x02\x03'
>>> s += bytes[[5]]     #or s = s + bytes[[5]]
>>> s
b'\x01\x02\x03\x05'

>>> s = b'\x01\x02\x03'
>>> s += bytes[chr[5], 'ascii']   ##or s = s + bytes[chr[5], 'ascii']
>>> s
b'\x01\x02\x03\x05'

Help on

>>> bytes[chr[5], 'ascii']
b'\x05'
0:

>>> print[bytes.__doc__]
bytes[iterable_of_ints] -> bytes
bytes[string, encoding[, errors]] -> bytes
bytes[bytes_or_buffer] -> immutable copy of bytes_or_buffer
bytes[int] -> bytes object of size given by the parameter initialized with null bytes
bytes[] -> empty bytes object

Construct an immutable array of bytes from:
  - an iterable yielding integers in range[256]
  - a text string encoded using the specified encoding
  - any object implementing the buffer API.
  - an integer

Or go for the mutable

>>> bytes[chr[5], 'ascii']
b'\x05'
4 if you need a mutable object and you're only concerned with the integers in range 0-256.

Creativecommons và DMCABài viết liên quanCắt [slice] list trong pythonChèn phần tử vào list python [insert, slice]Phương thức extend trong pythonĐảo ngược list trong python [reverse, reversed]Chuyển string sang list trong PythonKiểm tra và lấy phần tử trùng nhau trong list pythonSử dụng list comprehension trong PythonTạo list trong pythonHãy chia sẻ và cùng lan tỏa kiến thức lập trình Nhật Bản tại Việt Nam! HOME>> python cơ bản - lập trình python cho người mới bắt đầu>>10. list trong pythonBài sauTìm max và min trong mảng CBài tiếpPhương thức extend trong pythonBài viết mới nhấtTách chuỗi trong PHP [explode, preg_split] tháng 9 17, 2022Cắt chuỗi trong PHP [substr, mb_substr] tháng 9 17, 2022Tách chuỗi thành mảng trong PHP [explode] tháng 9 17, 2022Tìm kiếm chuỗi trong PHP [strpos] tháng 9 17, 2022Đếm số lần xuất hiện của ký tự và chuỗi trong chuỗi PHP [substr_count] tháng 9 17, 2022Chuyển chuỗi thành mảng trong PHP [str_split, mb_str_split] tháng 9 17, 2022Tách từng ký tự trong chuỗi PHP [substr, preg_split] tháng 9 17, 2022Xóa ký tự trong chuỗi PHP tháng 9 15, 2022if[typeof ez_ad_units != 'undefined']{ez_ad_units.push[[[300,250],'laptrinhcanban_com-leader-1','ezslot_15',560,'0','0']]};__ez_fad_position['div-gpt-ad-laptrinhcanban_com-leader-1-0'];

Hồ sơ

Bài Viết Liên Quan

Toplist mới

Bài mới nhất

Chủ Đề