Home > front end >  Vala - Bytes convert to string?
Vala - Bytes convert to string?

Time:07-06

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?

CodePudding user response:

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
  • Related