>>> s = "Hello World" >>> b = bytearray(s) >>> b bytearray(b'Hello World') >>> b[:5] = "Cruel" >>> b bytearray(b'Cruel World') >>> for i in b: ... print i ... 67 114 117 101 108 32 87 111 114 108 100 >>> b.append(33) >>> b bytearray(b'Cruel World!') >>> print b Cruel World! |
The usage
b"mystring"
was new to me as well. According to this SO answer:the b prefix for string literals is ineffective in 2.6, but it is a useful marker in the program, which flags explicitly the intent of the programmer to have the string as a data string rather than a text string. This info can then be used by the 2to3 converter or similar utilities when the program is ported to Py3k.More info here (thanks, Ned).
A bytearray has the methods of lists including
__getitem__
and __setitem__
. Unusually for Python, one can use arguments of both int and str types. The actual type of a bytearray item is < type 'int'>
.>>> b = bytearray('Hello World!') >>> b[-1] = '*' >>> b bytearray(b'Hello World*') >>> ord('!') 33 >>> b[-1] = 33 >>> b bytearray(b'Hello World!') |
We can use a bytearray to do a small job of encryption in a very convenient way:
>>> import numpy as np >>> msg = bytearray("Hello World") >>> n = len(msg) >>> L = np.random.randint(0,256,n) >>> key = bytearray(list(L)) >>> len(key) 11 >>> key bytearray(b'N\x963\x8a\x06\xf2\xe9\x92\xb9\xf4L') >>> >>> >>> ctext = bytearray() >>> for c,k in zip(msg,key): ... ctext.append(c ^ k) ... >>> ctext bytearray(b'\x06\xf3_\xe6i\xd2\xbe\xfd\xcb\x98(') >>> ptext = bytearray() >>> for c,k in zip(ctext,key): ... ptext.append(c ^ k) ... >>> ptext bytearray(b'Hello World') |