Re: [Vala] string <-> bytes conversion in Vala
- From: Jaroslav Šmíd <dataman64bit gmail com>
- To: Anatol Pomozov <anatol pomozov gmail com>
- Cc: vala <vala-list gnome org>
- Subject: Re: [Vala] string <-> bytes conversion in Vala
- Date: Tue, 04 Jan 2011 23:07:23 +0100
I've reported bug month ago about string conversion funtions (like
g_iconv). I don't know if they fixed it. Before you use it, check
bugzilla if solved (can't remember bug #).
On 12/30/2010 12:51 AM, Anatol Pomozov wrote:
Hi
On Thu, Dec 23, 2010 at 12:37 AM, Anatol Pomozov
<anatol pomozov gmail com> wrote:
Hi,
I manipulate with strings in my application such as storing/reading
to/from binary files. And I need to convert a string to/from array of
bytes. What is the best way of doing it in Vala?
Looking into string class http://valadoc.org/glib-2.0/string.html I
see a method called string#to_utf8() that converts string to array of
chars. But I cannot find any other methods.
So I have 2 questions:
- How to convert a string to array of chars in encoding different
from UTF8. Something like this one
http://download.oracle.com/javase/6/docs/api/java/lang/String.html#getBytes(java.nio.charset.Charset)
- How to convert array of chars/uint8 back to string? Like this one
http://download.oracle.com/javase/6/docs/api/java/lang/String.html#String(byte[],
java.nio.charset.Charset)
I think I found a way to convert string to bytes in Vala. Actually it
is as simple as casting string to char* and back. Under the hood
strings in vala is just a pointer to char.
Here is an example that shows it:
string hello = "abcd";
char* ch = (char*) hello;
stdout.printf("%c\n", ch[1]); // Second char, that is 'b'
char[] bytes = new char[4]; // 3 symbols and null-terminator
bytes[0] = '1';
bytes[1] = '2';
bytes[2] = '3';
bytes[3] = 0;
string str = (string)bytes;
stdout.printf(@"$str\n");
stdout.printf("%d\n", (int)str.length); // Prints '3'
WRT question 'how to convert strings to different encodings' I think
this should help
http://library.gnome.org/devel/glib/unstable/glib-Character-Set-Conversion.html
I haven't it used though.
_______________________________________________
vala-list mailing list
vala-list gnome org
http://mail.gnome.org/mailman/listinfo/vala-list
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]