Java UTF-8 processing method

zhaozj2021-02-16  139

/ *

* Returns an array of bytes representing the utf8 encoding

* of the specified string.

* /

Private staty [] getutf8bytes (string s) {

CHAR [] c = S.toChararray ();

INT LEN = C.LENGTH;

// count the number of encode bytes ...

INT count = 0;

For (int i = 0; i

INT CH = C [I];

IF (CH <= 0x7f) {

COUNT ;

} else IF (CH <= 0x7ff) {

COUNT = 2;

} else {

COUNT = 3;

}

}

// now return the encoded bytes ...

Byte [] b = new byte [count];

INT OFF = 0;

For (int i = 0; i

INT CH = C [I];

IF (CH <= 0x7f) {

B [OFF ] = (Byte) CH;

} else IF (CH <= 0x7ff) {

B [OFF ] = (BYTE) ((CH >> 6) | 0xc0);

B [OFF ] = (BYTE) ((CH & 0x3F) | 0x80);

} else {

B [OFF ] = (BYTE) ((CH >> 12) | 0xE0);

B [OFF ] = (Byte) ((CH> 6) & 0x3F) | 0x80);

B [OFF ] = (BYTE) ((CH & 0x3F) | 0x80);

}

}

Return B;

}

转载请注明原文地址:https://www.9cbs.com/read-8887.html

New Post(0)