Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void writeUTF8(String s) {
- int i = 0;
- for(char c : s.toCharArray())
- if(c >= 0x1 && c <= 0x7F)
- i++;
- else if(c == 0x0 || (c >= 0x80 && c <= 0x7FF))
- i += 2;
- else
- i += 3;
- int j = 0;
- byte[] b = new byte[i];
- for(char c : s.toCharArray())
- if(c >= 0x1 && c <= 0x7F)
- b[j++] = (byte)c;
- else if(c == 0x0 || (c >= 0x80 && c <= 0x7FF)) {
- b[j++] = (byte)(((c >> 6) & 0x1F) | 0xC0);
- b[j++] = (byte)((c & 0x3F) | 0x80);
- } else {
- b[j++] = (byte)(((c >> 12) & 0x0F) | 0xE0);
- b[j++] = (byte)(((c >> 6) & 0x0F) | 0x80);
- b[j++] = (byte)((c & 0x0F) | 0x80);
- }
- writeShort(b.length);
- write(b);
- }
Add Comment
Please, Sign In to add comment