Thread overview | |||||
---|---|---|---|---|---|
|
December 24, 2010 std.encoding Usage | ||||
---|---|---|---|---|
| ||||
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 |
December 24, 2010 Re: std.encoding Usage | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mandeep Singh Brar | On Fri, 24 Dec 2010 18:33:04 +0000 (UTC) Mandeep Singh Brar <mandeep@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 |
January 12, 2011 Re: std.encoding Usage | ||||
---|---|---|---|---|
| ||||
Posted in reply to spir | 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 |
Copyright © 1999-2021 by the D Language Foundation