std.encoding Usage

spir denis.spir at gmail.com
Fri Dec 24 10:51:11 PST 2010


On Fri, 24 Dec 2010 18:33:04 +0000 (UTC)
Mandeep Singh Brar <mandeep at brars.co.in> wrote:

> Hi,
> 
> Can you please help me/point me to the usage for std.encoding
> package.I think i was successfully able to encode a string into
> byte array using the following code:
>         string x = "test";
>         ubyte[] buffer;
> 	EncodingScheme es = EncodingScheme.create("UTF-16LE");
> 	foreach(c;codePoints(x))
>         {
> 		ubyte[4] buf;
> 	 	int l = es.encode(c, buf);
> 		buffer~=buf[0..l];
> 	}
> 
> and i was able to decode using the following:
>          string y;
> 	for(int pos=0;pos<buffer.length;pos+=2)
>         {
> 		ubyte[] cha;
> 		cha = buffer[pos..pos+2];
> 	 	dchar f1 = es.decode(cha);
> 		y~=cast(char)f1;
> 	}
> 
> However i dont think i am doing it the correct way, particularly
> the decoding part. (introducing 2 and creating another buffer
> etc.).. if i directly try decoding it using the buffer variable in
> the decoding example, i do not get a correctly decoded string.
> 
> Can you please let me know the correct way to encode and then
> decode a string using std.encoding package. I tried reading the doc
> page on the site but wasnt able to clearly follow it.

As an alternative, you may directly use std.utf instead (I also had issues using std.encoding, falled back to std.utf.) Example of encoding/decoding from/yo utf8 at https://bitbucket.org/denispir/denispir-d/src/b543fb352803/Text.d. Inside the struct Text, fromUTF8 decodes a whole source text and toString encodes it.
Note: a trick for decoding is that index (must be uint) is passed by ref and advanced by called funcs.


Denis
-- -- -- -- -- -- --
vit esse estrany ☣

spir.wikidot.com



More information about the Digitalmars-d-learn mailing list