Docsity

UTF-8 Encoding: Reading and Writing Strings in Binary Streams - Prof. Douglas J. Wokoun, Quizzes of Computer Science

Partial preview of the text

Download UTF-8 Encoding: Reading and Writing Strings in Binary Streams - Prof. Douglas J. Wokoun and more Quizzes Computer Science in PDF only on Docsity!

• Homework 5 due Wednesday. CS251 Intermediate Programming

Input/Output #5 Lecture 18

• Quiz 4 on I/O today.

uses as source

uses as sink

uses as source/sink

uses as source

uses as sink

Remember this

UTF-

knows ASCIIIf you open a UTF-16 document in a text editor that only Unicode, when encoded as 16-bit characters is called UTF-16.

 i  t   l  o  o  k  s   l  i  k  e   t  h  i  s .

bytes in the Unicode representation of ASCII symbols.The gaps (or squares) bet ween the letters are the zero

characters.identical to ASCII, but it is also able to contain Unicode For documents that only contain ASCII characters, UTF-8 islength encoding was developed called UTF-8.To bridge the gap bet ween ASCII and Unicode, a variable-

How UTF-8 encodes Unicode values:

0 a a a a a a a

0 a a a a a a a

a a b b b b b b

0 0 0 0 0 a a a

1 1 0 a a a a a

1 0 b b b b b b

b b c c c c c c

1 1 1 0 a a a aa a a a b b b b

1 0 b b b b b b

1 0 c c c c c c

Encoding “Hello, World!” in UTF-8:   

e2 80 9c

201c 0048 00e9 006c 006c 006f 002c

201d

H é l l o ,

0057 006f 0072 006c 0064 0021 W o r l d!

201c 0048 00e9 006c 006c 006f 002c

201d

H é l l o ,

0057 006f 0072 006c 0064 0021 W o r l d!

Encoding “Hello, World!” in UTF-8:  

e2 80 9c

201c 0048 00e9 006c 006c 006f 002c

201d

H é l l o ,

0057 006f 0072 006c 0064 0021 W o r l d!

Encoding “Hello, World!” in UTF-8:  

e2 80 9c

c7 a

6c

simplicity, express your answers in hex:Convert the following UTF-16 characters to UTF-8. For

Hex

Binary

0

0

1

1

2

10

3

11

4

100

5

101

6

110

7

111

8

1000

9

1001

A

1010

B

1011

C

1100

D

1101

E

1110

F

1111

Character range

Encoding (binary)

0x007f)

00000000 0bbbbbbb

0bbbbbbb

0x07ff)

00000aaa aabbbbbb

110aaaaa 10bbbbbb

0x0800)

aaaabbbb bbcccccc

1110aaaa 10bbbbbb 10cccccc

0065 046c

47df

Text Within Binary Streams

you want to serialize When you are serializing the state of your objects, sometimes

String

s as well as primitive types.

DataInputStream While we didn't talk about it at the time we discussed

/

DataOutputStream

, these classes have

methods to support reading and writing

String

s to and from

dos.writeUTF("string") binary streams.

writes a

String

to the stream.

dis.readUTF()

reads a

String

from the stream.

UTF-8 is the only text format available.

the binary stream ends,To be able to determine where the text data embedded in

writeUTF()

sends a

short

(t wo-

UTFFormatException Embedded UTF-8 Strings are limited to 64K, andbytes in the UTF-8 string.byte) value into the stream giving the total number of

is thrown if the String is too long.

UTFFormatException

is a subclass of

IOException

.