먼저 (n
말은) 단순히 널 바이트 n
길이의 바이트 문자열을 반환합니다.
>>> bytes([5]) #This will work only for range 0-256.
b'\x05'
을 또는 :
어느 쪽이든 당신은 할 수 있습니다 :
>>> bytes(chr(5), 'ascii')
b'\x05'
이미 바이트, 그래서 불변 업데이트 할 것을 언급 @simonzack으로 (또는 더 나은 그래서, 당신이 여기 원하는 게 아니에요 새 문자열에 다시 할당한다고 말하면) 그 값은 +=
연산자를 사용해야합니다. bytes()
에
>>> 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'
도움말 :
>>> 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
아니면 변경 가능한 객체가 필요하고이 범위 0-256의 정수 만 우려 경우 변경 가능한 bytearray
에 대한 이동합니다.
'bytearray'는 0-256 범위의 정수만 저장할 수 있습니다. 이 경우 OP는 동일한 범위에만 관심이 있지만 :-) –
'bytearray'는'xs.extend (b '\ x11 \ x22 \ x33')처럼 더 많은 바이트를 한꺼번에 추가하는'.extend' 메소드를 가지고 있습니다. – pepr