Guest User

Untitled

a guest
Apr 22nd, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.73 KB | None | 0 0
  1. public void writeUTF8(String s) {
  2. int i = 0;
  3. for(char c : s.toCharArray())
  4. if(c >= 0x1 && c <= 0x7F)
  5. i++;
  6. else if(c == 0x0 || (c >= 0x80 && c <= 0x7FF))
  7. i += 2;
  8. else
  9. i += 3;
  10. int j = 0;
  11. byte[] b = new byte[i];
  12. for(char c : s.toCharArray())
  13. if(c >= 0x1 && c <= 0x7F)
  14. b[j++] = (byte)c;
  15. else if(c == 0x0 || (c >= 0x80 && c <= 0x7FF)) {
  16. b[j++] = (byte)(((c >> 6) & 0x1F) | 0xC0);
  17. b[j++] = (byte)((c & 0x3F) | 0x80);
  18. } else {
  19. b[j++] = (byte)(((c >> 12) & 0x0F) | 0xE0);
  20. b[j++] = (byte)(((c >> 6) & 0x0F) | 0x80);
  21. b[j++] = (byte)((c & 0x0F) | 0x80);
  22. }
  23. writeShort(b.length);
  24. write(b);
  25. }
Add Comment
Please, Sign In to add comment