ByteArray
Bytes
  • an immutable (can be modified) sequence of integers in the range 0 <=x < 256, the elements of a bytes object cannot be changed
  • str in Python 2 is byte string
  • #!/usr/bin/python
    
    d = b'Python How to Program';
    
    print type(d), d #'str', Python How to Program
    
    d2 = bytes([97, 98, 99, 100]); #save whole list as string
    print type(d2), str(d2); #'str', [97, 98, 99, 100]
    print d2[0]; #[
    		
    ByteArray
  • a mutable (can be modified) sequence of integers in the range 0 <=x < 256
  • Bytes and bytearrays are an efficient, byte-based form of strings
  • #!/usr/bin/python
    
    # convert list to bytearray
    arr = bytearray([97, 98, 99, 99]);
    
    # convert bytearray to bytes
    print bytes(arr); #abcc
    
    # modify elements
    arr[0] = 10;
    
    # access element
    print arr[0]
    
    # slicing
    arr2 = arr[:2];
    		
    #!/usr/bin/python
    # -*- coding: utf-8 -*-
    
    import sys;
    
    # set up default encoding
    reload(sys)  # Reload does the trick!
    sys.setdefaultencoding('UTF8')
    print sys.stdout.encoding;
    
    arr = bytearray("Café", "utf-8");
    
    # convert to string
    print type(str(arr)), str(arr);
    
    #convert to unicode
    print type(arr.decode('utf-8')), arr.decode('utf-8');
    
    #count
    print arr.count(b'a');
    
    # in
    if 97 in arr:
        print 'a in arr ...'
    
    # concatenation
    print arr+bytearray(b'dddddddd');
    
    #conver to list
    print list(arr); #[67, 97, 102, 195, 169]
    
    #append
    arr.append(97);
    
    #delete
    del arr[:2];
    
    #insert
    arr.insert(0, 98);
    print arr;
    
    #replace
    arr = arr.replace(b'b', b'c');
    print arr;
    		
    Reference
  • Bytes and Bytearray