[whatwg] API for encoding/decoding ArrayBuffers into text
    NARUSE, Yui 
    naruse at airemix.jp
       
    Thu Mar 22 07:37:24 PDT 2012
    
    
  
2012/3/22 Anne van Kesteren <annevk at opera.com>:
> As for the API, how about:
>
>  enc = new Encoder("euc-kr")
>  string1 = enc.encode(bytes1)
>  string2 = enc.encode(bytes2)
>  string3 = enc.eof() // might return empty string if all is fine
>
> And similarly you would have
>
>  dec = new Decoder("shift_jis")
>  bytes = dec.decode(string)
>
> Or alternatively you could have a single object that exposes both encode()
> and decode() and tracks state for both:
>
>  enc = new Encoding("gb18030")
>  bytes1  = enc.decode(string1)
>  string2 = enc.encode(bytes2)
Usually, strings are encoded to bytes.
Therefore that encode/decode methods should be reversed like:
 enc = new Encoding("gb18030")
 bytes1  = enc.encode(string1)
 string2 = enc.decode(bytes2)
Or if it may cause confusion use getBytes/getChars like Java and C#.
http://docs.oracle.com/javase/7/docs/api/java/lang/String.html
http://msdn.microsoft.com/en-us/library/system.text.encoder(v=vs.110).aspx#Y1873
http://msdn.microsoft.com/en-us/library/system.text.Decoder(v=vs.110).aspx#Y1873
 enc = new Encoding("gb18030")
 bytes1  = enc.getBytes(string1)
 string2 = enc.getChars(bytes2)
-- 
NARUSE, Yui  <naruse at airemix.jp>
    
    
More information about the whatwg
mailing list