The file contains integers in decimal form. Convert each number to binary form. Encrypt data in such a way that every eight binary characters turn into a letter of the Latin alphabet. The table of correspondence of codes to letters is contained in the second file. Provide a function to perform the decryption.

If the file should store a table of correspondence of numbers to letters, for example

11001100 A 11010111 B 11110101 C 

how do i put them in the table in the program

 { ['10010011']='A', ['10110000']='B', ... } 

what would then compare them with the already existing numbers, and subsequently replace the number with a letter?

  function tobin(num) local tmp = {} repeat tmp[#tmp+1]=num%2 num = math.floor(num/2) until num==0 return table.concat(tmp):reverse() end local function main() io.input("number.txt") while true do local val = io.read("*number") if val == nil then break end local a = {} a = tobin(val) print(tostring(a)) end ...... end main() 

1 answer 1

how do i put them in the table in the program

for example:

 local a = {} -- объявление должно быть за пределами цикла io.input("имя.файла") -- открываем файл на чтение while true do local line = io.read("*line") -- читаем целую строку if line == nil then break end -- если ничего не прочиталось - -- конец цикла x,y = string.match(line,"([01]+) (%a)") -- двоичные цифры -- помещаем в x, букву - в y a[x]=y -- добавляем эту пару в ассоциативный массив end -- отладочный вывод for k,v in pairs(a) do print("["..k.."]="..v) end --[[ при входных данных: 11001100 A 11010111 B 11110101 C напечатает: [11001100]=A [11010111]=B [11110101]=C --]] 

update

since the tables in lua are implemented as associative “key / value” arrays , then a nested table can be used to store two arbitrary values ​​in the table row:

 local a = {} -- объявление должно быть за пределами цикла local n = 1 -- инициализируем счётчик элементов таблицы io.input("имя.файла") -- открываем файл на чтение while true do local line = io.read("*line") -- читаем целую строку if line == nil then break end -- если ничего не прочиталось - -- конец цикла x,y = string.match(line,"([01]+) (%a)") -- двоичные цифры -- помещаем в x, букву - в y a[n]={ x,y } -- добавляем эту пару в таблицу в виде -- вложенной таблицы n=n+1 -- увеличиваем счётчик end -- отладочный вывод for k,v in pairs(a) do print(v[1].."="..v[2]) -- обращаемся к элементам вложенной -- таблицы по их номерам end --[[ при входных данных: 11001100 A 11010111 B 11110101 C 11001100 D 11111110 E 11001001 F напечатает: 11001100=A 11010111=B 11110101=C 11001100=D 11111110=E 11001001=F --]] 
  • Thanks, but here is the file 11001100 A 11010111 B 11110101 C 11001100 D 11111110 E 11001001 F and the output is [11001100] = D [11001001] = F [11010111] = B [11110101] = C [11111110] = E - PL
  • it turns out the output is not in the sequence and still skips the first line - PL
  • 1. about the "inconsistency" - apparently, this is the implementation of the associative array in lua . see the article about implementation . 2. about "skips" - for the letters A and D you have the same key: 11001100 . accordingly, the last value is preserved: D - aleksandr barakin
  • perhaps it makes sense for you to use a letter as a key, not a number. Well, once the numbers can be repeated for different letters. - aleksandr barakin
  • @PL, added the answer. - aleksandr barakin