Advertisement
qwertyMAN_rus

LuaLib bin-dec

Jan 7th, 2016 (edited)
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.56 KB | None | 0 0
  1. -- Название библиотеки: Переводчик систем счисления.
  2. -- Автор: qwertyMAN
  3. -- P.S. Можно переводить нестандартные системы счисления, с другими алфавитами и прочее.
  4. -- Возможности безграничны, пополнять таблицу можно сколько угодно.
  5. -- Примечание: lua, число вида 0x000000 (hex) будет автоматически переведить в десятичную систему счисления, без каких либо функций
  6.  
  7.  
  8. local conv = {}
  9.  
  10. -- вспомогательная таблица
  11. local table={}
  12. table[10] = "A"
  13. table[11] = "B"
  14. table[12] = "C"
  15. table[13] = "D"
  16. table[14] = "E"
  17. table[15] = "F"
  18. table.A = 10
  19. table.B = 11
  20. table.C = 12
  21. table.D = 13
  22. table.E = 14
  23. table.F = 15
  24.  
  25. -- Переводит десятичный вид в N-систему счисления
  26. function conv.aEngine(input, n)
  27.     local output=""
  28.     while true do
  29.         local cheloe = math.modf(input/n)   -- найдём целое число
  30.         if cheloe == 0 then                 --   END   --выход из цикла если вычисление завершено
  31.             if table[input] then            -- THE END -- записывает последний символ в строку и возвращает значение
  32.                 return  table[input]..output
  33.             else
  34.                 return  input..output
  35.             end
  36.         else
  37.             local ost = tonumber(input) - cheloe*-- найдём остаток от деления
  38.             if table[ost] then                      -- запишем остаток как первый символ в последовательности
  39.                 output = table[ost]..output
  40.             else
  41.                 output = ost..output
  42.             end
  43.             input = cheloe
  44.         end
  45.     end
  46. end
  47.  
  48. -- Переводит N-систему счисления в десятичную
  49. function conv.Engine(input, n)
  50.     if n==16 then input=string.upper(input) end
  51.     local sum = 0                           -- Сумма
  52.     local num = string.len(input)           -- Количество цифр в введённом числе
  53.     for i=1, num do
  54.         local number = string.sub(input, -i, -i)
  55.         if table[number] then
  56.             sum = sum + table[number]*n^(i-1)
  57.         else
  58.             sum = sum + tonumber(number)*n^(i-1)
  59.         end
  60.     end
  61.     return sum
  62. end
  63.  
  64. function conv.bin(input)
  65.     return conv.Engine(input, 2)
  66. end
  67.  
  68. function conv.oct(input)
  69.     return conv.Engine(input, 8)
  70. end
  71.  
  72. function conv.hex(input) -- принимает string. Не использовать для значения вида 0x000000
  73.     return conv.Engine(input, 16)
  74. end
  75.  
  76. function conv.abin(input)
  77.     return conv.aEngine(input, 2)
  78. end
  79.  
  80. function conv.aoct(input)
  81.     return conv.aEngine(input, 8)
  82. end
  83.  
  84. function conv.ahex(input) -- возвращает string
  85.     return conv.aEngine(input, 16)
  86. end
  87.  
  88. function conv.getThreeColor(input) -- принимает как текстовый HEX формат, так и числовой 0x000000, в том числе и простые цифры.
  89.     if type(input)=="string" then input=conv.hex(input) end
  90.     local r = math.modf(input/65536)
  91.     input = input-r*65536
  92.     local g = math.modf(input/256)
  93.     b = input-g*256
  94.     return r, g, b
  95. end
  96.  
  97. function conv.getColor(r, g, b) -- принимает как текст, так и числа. Выводит числа.
  98.     if type(r)=="string" then r=conv.hex(r) end
  99.     if type(g)=="string" then g=conv.hex(g) end
  100.     if type(b)=="string" then b=conv.hex(b) end
  101.     local output = r*65536+g*256+b
  102.     return output
  103. end
  104.  
  105. function conv.invertColor(input)
  106.     return 0xffffff-input
  107. end
  108.  
  109. return conv
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement