Search code examples
stringbytevala

Vala - Bytes convert to string?


I have a GLib.Bytes object.

I want to print it and use it as a string like this:

Bytes bytes = new Bytes({65, 66, 67});

print(bytes); // <-- ERROR

How can I convert it to string?


Solution

  • Get raw byte array uint8[] with Bytes.get_data() and cast it as string.

    Example:

    Bytes bytes = new Bytes({65, 66, 67});
    
    string str = (string)bytes.get_data();
    print(str);
    

    Output:

    ABC