Skip to content

Commit f388f6f

Browse files
authored
Merge pull request #292 from longo-andrea/article/text-decoder-and-encoder
TextDecoder and TextEncoder
2 parents a95bdc7 + 8568eaf commit f388f6f

File tree

1 file changed

+22
-22
lines changed

1 file changed

+22
-22
lines changed

4-binary/02-text-decoder/article.md

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
1-
# TextDecoder and TextEncoder
1+
# TextDecoder e TextEncoder
22

3-
What if the binary data is actually a string? For instance, we received a file with textual data.
3+
E se il dato binario in realtà fosse una stringa? Ad esempio, se ricevessimo un file contente dati testuali.
44

5-
The build-in [TextDecoder](https://encoding.spec.whatwg.org/#interface-textdecoder) object allows to read the value into an actual JavaScript string, given the buffer and the encoding.
5+
L'oggetto integrato [TextDecoder](https://encoding.spec.whatwg.org/#interface-textdecoder), dato il buffer e l'encoding, ci consente di leggere il valore come se fosse una stringa JavaScript.
66

7-
We first need to create it:
7+
Come prima cosa dobbiamo crearlo:
88
```js
99
let decoder = new TextDecoder([label], [options]);
1010
```
1111

12-
- **`label`** -- the encoding, `utf-8` by default, but `big5`, `windows-1251` and many other are also supported.
13-
- **`options`** -- optional object:
14-
- **`fatal`** -- boolean, if `true` then throw an exception for invalid (non-decodable) characters, otherwise (default) replace them with character `\uFFFD`.
15-
- **`ignoreBOM`** -- boolean, if `true` then ignore BOM (an optional byte-order Unicode mark), rarely needed.
12+
- **`label`**, l'encoding di default è `utf-8`, ma sono supportati anche `big5`, `windows-1251` e molti altri.
13+
- **`options`**, oggetto opzionale:
14+
- **`fatal`**, boolean, se vale `true` allora verrà generata un'eccezione per i caratteri invalidi (non-decodificabili), altrimenti (default) verranno rimpiazzati con il carattere `\uFFFD`.
15+
- **`ignoreBOM`**, boolean, se vale `true` allora ignora BOM (un marcatore opzionale di byte-order Unicode), raramente utilizzato.
1616

17-
...And then decode:
17+
...E successivamente decodificare:
1818

1919
```js
2020
let str = decoder.decode([input], [options]);
2121
```
2222

23-
- **`input`** -- `BufferSource` to decode.
24-
- **`options`** -- optional object:
25-
- **`stream`** -- true for decoding streams, when `decoder` is called repeatedly with incoming chunks of data. In that case a multi-byte character may occasionally split between chunks. This options tells `TextDecoder` to memorize "unfinished" characters and decode them when the next chunk comes.
23+
- **`input`**, il `BufferSource` da decodificare.
24+
- **`options`**, oggetto opzionale:
25+
- **`stream`**, `true` per la decodifica di stream, quando `decoder` viene invocato ripetutamente con blocchi di dati in arrivo. In questo caso un carattere multi-byte potrebbe venir diviso in blocchi. Questa opzione dice al `TextDecoder` di memorizzare i caratteri "incompleti" e di decodificarli all'arrivo del prossimo blocco.
2626

27-
For instance:
27+
Ad esempio:
2828

2929
```js run
3030
let uint8Array = new Uint8Array([72, 101, 108, 108, 111]);
@@ -39,34 +39,34 @@ let uint8Array = new Uint8Array([228, 189, 160, 229, 165, 189]);
3939
alert( new TextDecoder().decode(uint8Array) ); // 你好
4040
```
4141

42-
We can decode a part of the buffer by creating a subarray view for it:
42+
Possiamo decodificare una parte del buffer creando un visualizzatore su un suo sotto-array:
4343

4444

4545
```js run
4646
let uint8Array = new Uint8Array([0, 72, 101, 108, 108, 111, 0]);
4747

48-
// the string is in the middle
49-
// create a new view over it, without copying anything
48+
// la stringa sta al centro
49+
// ne crea un nuovo visualizzatore, senza copiare nulla
5050
let binaryString = uint8Array.subarray(1, -1);
5151

5252
alert( new TextDecoder().decode(binaryString) ); // Hello
5353
```
5454

5555
## TextEncoder
5656

57-
[TextEncoder](https://encoding.spec.whatwg.org/#interface-textencoder) does the reverse thing -- converts a string into bytes.
57+
[TextEncoder](https://encoding.spec.whatwg.org/#interface-textencoder) fa esattamente l'operazione inversa, converte stringe in byte.
5858

59-
The syntax is:
59+
La sintassi è:
6060

6161
```js
6262
let encoder = new TextEncoder();
6363
```
6464

65-
The only encoding it supports is "utf-8".
65+
L'unica codifica che supporta è "utf-8".
6666

67-
It has two methods:
68-
- **`encode(str)`** -- returns `Uint8Array` from a string.
69-
- **`encodeInto(str, destination)`** -- encodes `str` into `destination` that must be `Uint8Array`.
67+
Mette a disposizione due metodi:
68+
- **`encode(str)`**, ritorna `Uint8Array` partendo da una stringa.
69+
- **`encodeInto(str, destination)`**, esegue l'encode di `str` in `destination` il quale deve essere un `Uint8Array`.
7070

7171
```js run
7272
let encoder = new TextEncoder();

0 commit comments

Comments
 (0)