(ES6) ES6 (2016)

TypedArr (TB09)

   1:  const str = "Hello, world!";
   2:  const encoder = new TextEncoder();
   3:  const encoded = encoder.encode(str); // Encode string as UTF-8
   4:  
   5:  const buffer = new ArrayBuffer(encoded.length);
   6:  const view = new DataView(buffer);
   7:  
   8:  
   9:  for (let i = 0; i < encoded.length; i++) {
  10:    view.setUint8(i, encoded[i]);
  11:  }
  12:  
  13:  const decoder = new TextDecoder('utf-8'); // Specify encoding
  14:  const decoded = decoder.decode(buffer);
  15:  
  16:  console.log(decoded); // Output: Hello, world!
  17:  



Working with Strings (UTF-8 Encoding):

This example demonstrates how you can encode/decode UTF-8 strings and store them in an ArrayBuffer using DataView.
TextEncoder converts the string to UTF-8 byte array, and TextDecoder converts back from UTF-8 array to string.
It shows integration of DataView with other API for character encoding.

Hello, world!





TypedArr context:






ES6 context:




Binary context:



Comments ( )
<00>  <01>  <02>  <03>  <04>  <05>  <06>  <07>  <08>  <09>  <10>  <11>  <12>  <13>  <14>  <15>  <16>  <17>  <18>  <19>  <20>  <21>  <22>  <23>  <24>  <25
Link to this page: http://www.vb-net.com/JavascriptES6/TB09.htm
<TAGS>  <ARTICLES>  <FRONT>  <CORE>  <MVC>  <ASP>  <NET>  <DATA>  <TASK>  <XML>  <KIOSK>  <NOTES>  <SQL>  <LINUX>  <MONO>  <FREEWARE>  <DOCS> <TRAVELS> <FLOWERS> <RESUME> < THANKS ME>