Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
十进制到十六进制转换器Lua_Lua_Hex_Decimal - Fatal编程技术网

十进制到十六进制转换器Lua

十进制到十六进制转换器Lua,lua,hex,decimal,Lua,Hex,Decimal,我开始在Lua中制作一个十进制到十六进制的翻译程序来尝试我所知道的东西;我读过Lua可以自己做到这一点,但我还是想重新创造它 我犯了一个巨大的错误,我不确定问题出在哪里: file = "log"..os.date("%S.%M.%H%p.%d.%m.%Y")..".txt" do log = io.open(file, "w") io.output(log) end dechexint = remainder function hexTracker() remaind

我开始在Lua中制作一个十进制到十六进制的翻译程序来尝试我所知道的东西;我读过Lua可以自己做到这一点,但我还是想重新创造它

我犯了一个巨大的错误,我不确定问题出在哪里:

file = "log"..os.date("%S.%M.%H%p.%d.%m.%Y")..".txt"
do
    log =  io.open(file, "w")
    io.output(log)
end

dechexint = remainder

function hexTracker()
  remainder = result % 16
  result = result / 16
  print(remainder, "\n")
  io.write(remainder, "\n")
end

function hexTranslator()
  if remainder == 11 
  then print("A")
  io.write("A")

  else
    if dechexint == 12
    then print("B")
    io.write("B")

    else
      if dechexint == 13
      then print("C")
      io.write("C")

      else
        if dechexint == 14
        then print("D")
        io.write("D")

        else
          if dechexint == 15
          then print("E")
          io.write("E")

          else 
            if dechexint == 16
            then print("F")
            io.write("F")
end
end
end
end
end
end
end

decint = io.read("*number")

  result = decint / 16
  result = decint % 16

while(result>0.1)
 do
   hexTracker()
   hexTranslator()
end
它在技术上是可行的,但是

input: 
4579

output:
3
0.1875

它应该给出11E3;有人能帮忙吗?

有一些严重的问题。它无法通过数字的数字循环。它在十六进制数字映射上不匹配。它无法正确打印十进制数字。
hexTracker
hexttranslator
函数之间的关系不起作用。每一个都会阻止它工作。此外,还有一些较小的问题不会彻底破坏它,但会导致可维护性较差的代码。其中包括大量使用全局变量、深度嵌套的
if/else if
构造、多余的
do
构造、使用无参数过程等函数

我试图让这个答案在结构上尽可能接近你所拥有的,这样你就可以更容易地看到我改变了什么以及它是如何工作的。我更专注于制作一些你可以从中学习的东西,而不是在各个方面都很完美的东西,我保留了你的
io.read()
输入数据和输出日志文件的方法,使其具有相同的名称,因此它的运行完全相同

我清理了一些构造,并以(希望)透明的方式简化了它们。我删除了
hexTracker()
函数,但使
hexttranslator()
递归,以便它能遍历所有数字。除此之外,我还使用了一个参数和一个返回值

您可以在此处看到结果:

file = "/tmp/log"..os.date("%S.%M.%H%p.%d.%m.%Y")..".txt"
log =  io.open(file, "w")
io.output(log)

function hexTranslator(result)
  if result >= 16 then
    local intQuotient = math.floor(result / 16)
    local remainder = result % 16
    return hexTranslator(intQuotient) .. hexTranslator(remainder)
  else
    if result == 10 then
      io.write("A")
      return "A"
    elseif result == 11 then
      io.write("B")
      return "B" 
    elseif result == 12 then
      io.write("C")
      return "C"
    elseif result == 13 then
      io.write("D")
      return "D"
    elseif result == 14 then
      io.write("E")
      return "E"
    elseif result == 15 then
      io.write("F")
      return "F"
    else
      io.write(result)
      return tostring(result)
    end
  end
end

decint = io.read("*number")
print(hexTranslator(decint))
有一些方法可以使这个解决方案变得更好,值得一提的是用表查找替换整个
if/elseif
构造。这是更短、更容易阅读和更快地执行。这里有一个快速的方法来做到这一点,保持其他一切不变:

file = "/tmp/log"..os.date("%S.%M.%H%p.%d.%m.%Y")..".txt"
log =  io.open(file, "w")
io.output(log)

function hexTranslator(result)
  hexDigits = {[10] = "A", [11] = "B", [12] = "C", [13] = "D", [14] = "E", [15] = "F"}
  if result >= 16 then
    local intQuotient = math.floor(result / 16)
    local remainder = result % 16
    return hexTranslator(intQuotient) .. hexTranslator(remainder)
  else
    io.write(result)
    return hexDigits[result] or tostring(result)
  end
end

decint = io.read("*number")
print(hexTranslator(decint))
我希望这有帮助。一旦您熟悉了它,您可能会想要删除日志记录,并且除了您现在使用的方法之外,您可能还想研究支持命令行输入数字之类的选项

print(('%X'):format(12345678)) -- BC614E
小心溢出(0x100000000)



Lua有一个
elseif
关键字,以避免像您的代码那样嵌套
else
。此外,尝试使用局部变量和参数传递函数重写代码,现在太麻烦了。
function tohex(num)
    local charset = {"0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"}
    local tmp = {}
    repeat
        table.insert(tmp,1,charset[num%16+1])
        num = math.floor(num/16)
    until num==0
    return table.concat(tmp)
end

print(tohex(0)) -- 0
print(tohex(1234567890123456))  -- 462d53c8abac0