Guest User

Untitled

a guest
Dec 12th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.54 KB | None | 0 0
  1. -- Implicit indexes of tuples (number<->string conversion)
  2. do
  3. local function t2i(s)
  4. local i = 0
  5. for k = 1, #s do
  6. i = i * 256 + s:byte(k)
  7. end
  8. return i
  9. end
  10.  
  11. local function i2t(i)
  12. local a = {0}
  13. if i < 256 then
  14. return string.char(i)
  15. elseif i > 1 then
  16. local k = math.ceil(math.log(i+.5)/math.log(256))
  17. while i > 0 do
  18. a[k] = i % 256
  19. i = math.floor(i / 256)
  20. k = k - 1
  21. end
  22. end
  23. return string.char(unpack(a))
  24. end
  25.  
  26. return {
  27. t2i = t2i,
  28. i2t = i2t
  29. }
  30. end
Add Comment
Please, Sign In to add comment