std.encoding Usage

Mandeep Singh Brar mandeep at brars.co.in
Fri Dec 24 10:33:04 PST 2010


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.

Regards
Mandeep


More information about the Digitalmars-d-learn mailing list