from gi.repository import GLib data = b'\x00\x01\x02\x03' byte_array = GLib.Bytes.new(data) print(byte_array.get_size()) # outputs 4 byte_array.append(b'\x04\x05\x06\x07') print(byte_array.get_size()) # outputs 8 print(byte_array.get_data()) # outputs b'\x00\x01\x02\x03\x04\x05\x06\x07'
from gi.repository import GLib string = 'hello world' byte_array = GLib.Bytes(string.encode()) print(byte_array.get_data()) # outputs b'hello world' decoded_string = byte_array.get_data().decode() print(decoded_string) # outputs 'hello world'This code example shows how to convert a string to a byte array using the `encode` method and then convert it back to a string using the `decode` method. The `get_data` method is used to retrieve the raw binary data from the byte array. Package library: python-gobject