www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - std.encoding Usage

reply Mandeep Singh Brar <mandeep brars.co.in> writes:
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
Dec 24 2010
parent reply spir <denis.spir gmail.com> writes:
On Fri, 24 Dec 2010 18:33:04 +0000 (UTC)
Mandeep Singh Brar <mandeep brars.co.in> wrote:

 Hi,
=20
 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 =3D "test";
         ubyte[] buffer;
 	EncodingScheme es =3D EncodingScheme.create("UTF-16LE");
 	foreach(c;codePoints(x))
         {
 		ubyte[4] buf;
 	 	int l =3D es.encode(c, buf);
 		buffer~=3Dbuf[0..l];
 	}
=20
 and i was able to decode using the following:
          string y;
 	for(int pos=3D0;pos<buffer.length;pos+=3D2)
         {
 		ubyte[] cha;
 		cha =3D buffer[pos..pos+2];
 	 	dchar f1 =3D es.decode(cha);
 		y~=3Dcast(char)f1;
 	}
=20
 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.
=20
 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 f= rom/yo utf8 at https://bitbucket.org/denispir/denispir-d/src/b543fb352803/T= ext.d. Inside the struct Text, fromUTF8 decodes a whole source text and toS= tring encodes it. Note: a trick for decoding is that index (must be uint) is passed by ref an= d advanced by called funcs. Denis -- -- -- -- -- -- -- vit esse estrany =E2=98=A3 spir.wikidot.com
Dec 24 2010
parent Mandeep Singh Brar <mandeep brars.co.in> writes:
Putting here that the following worked for me.

string str
ubyte[] buffer;
foreach(ch;str) {
	ubyte[4] buf;
	int len = encodingScheme.encode(ch, buf);
	buffer~=buf[0..len];
}


ubyte[] encodedBuffer;
string decodedString;
while(encodedBuffer.length>0) {
decodedString~=cast(char)encodingScheme.safeDecode(encodedBuffer);
}

Thanks
Mandeep
Jan 12 2011