Guest User

Untitled

a guest
Jan 19th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.33 KB | None | 0 0
  1. # scott nicholas (neutron@scottn.us) 2011. public domain.
  2.  
  3. # take input, show UTF-8 and try to display it.
  4. {
  5. chr = conv_unicode_to_utf8($0, arr);
  6. printf("[%s]=[%s]=[%s]\n", $0, arr[0], chr);
  7. }
  8.  
  9. function conv_init( i, j, c, h, a)
  10. {
  11. __conv_init = 1
  12. split("1 2 3 4 5 6 7 8 9 A B C D E F", __hextab, " ")
  13. split("0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111", __bintab, " ")
  14. __hextab[0] = 0
  15. __bintab[0] = "0000"
  16. for (i = 1; i <= 255; ++i)
  17. __chr2hex[sprintf("%c", i)""] = sprintf("%02X", i)
  18. for (i = 0; i < 16; i++) {
  19. __hex2bin[__hextab[i]] = __bintab[i]
  20. __bin2dec[__bintab[i]] = i
  21. __bin2hex[__bintab[i]] = __hextab[i]
  22. # __hex2dec[__hextab[i]] = i
  23. }
  24. }
  25.  
  26. function conv_hex2bin(hex,
  27. i, bin)
  28. {
  29. if (!__conv_init) conv_init()
  30. for (i = 1; i <= length(hex); i++)
  31. bin = bin __hex2bin[toupper(substr(hex, i, 1))]
  32. return bin
  33. }
  34.  
  35. function conv_bin2chr(bin,
  36. i, chr)
  37. {
  38. if (!__conv_init) conv_init()
  39. for (i = 1; i <= length(bin); i += 8)
  40. {
  41. chr = chr sprintf("%c", \
  42. 16 * __bin2dec[substr(bin, i, 4)] + \
  43. __bin2dec[substr(bin, i+4, 4)])
  44. }
  45. return chr
  46. }
  47.  
  48. function conv_bin2hex(bin,
  49. i, hex)
  50. {
  51. if (!__conv_init) conv_init()
  52. for (i = 1; i <= length(bin); i += 4)
  53. hex = hex __bin2hex[substr(bin, i, 4)]
  54. return hex
  55. }
  56.  
  57. function conv_unicode_to_utf8(hex, arr,
  58. bin, blen, utf8)
  59. {
  60. if (length(hex) != 4) return "" # i dunno
  61.  
  62. # hex2bin
  63. bin = conv_hex2bin(hex)
  64. blen = 17 - index(bin, "1")
  65.  
  66. # then add UTF-8 prefixes. we have 2 bytes right now.
  67. if (blen > 11)
  68. utf8 = "1110" substr(bin, 1, 4) "10" substr(bin, 5, 6) \
  69. "10" substr(bin, 11, 6)
  70. else if (blen > 7)
  71. utf8 = "110" substr(bin, 6, 5) "10" substr(bin, 11, 6)
  72. else
  73. utf8 = substr(bin, 9)
  74.  
  75. # pass back in array too
  76. arr[0] = conv_bin2hex(utf8)
  77.  
  78. # then back to ... characters? :O
  79. return conv_bin2chr(utf8)
  80. }
Add Comment
Please, Sign In to add comment