无法转换十六进制字符串(在Lua中)

时间:2013-09-01 23:26:54

标签: string function lua hex readable

我一直在尝试不同的方法来读取这个十六进制字符串,我无法弄清楚如何。每种方法只转换其中的一部分。在线转换器没有这样做,这是我尝试的方法:

function string.fromhex(str)
  return (str:gsub('..', function (cc)
    return string.char(tonumber(cc, 16))
  end))
end

packedStr = "1b4c756151000104040408001900000040746d702f66756e632e416767616d656e696f6e2e6c756100160000002b0000000000000203000000240000001e0000011e008000000000000100000000000000170000002900000000000008410000000500000006404000068040001a400000160000801e0080000a00800041c0000022408000450001004640c1005a40000016800c80458001008500000086404001868040018600420149808083458001008580020086c04201c5000000c640c001c680c001c600c301014103009c80800149808084450001004980c38245c003004600c4005c408000458002004640c40085800400860040018640400186c0440186004201c14003005c00810116000280450105004641c50280010000c00100025c8180015a01000016400080420180005e010001614000001600fd7f4580050081c005005c400001430080004700060043008000478001004300800047c003001e008000190000000405000000676d63700004050000004368617200040700000053746174757300040b000000416767616d656e696f6e000404000000746d7000040f000000636865636b65645f7374617475730004030000007477000409000000636861724e616d6500040900000066756c6c6e616d6500040a0000006775696c644e616d65000407000000737472696e670004060000006d617463680004060000006775696c6400040400000025772b0001010403000000756900040d00000064726177456c656d656e7473000407000000676d617463680004030000005f470004050000004e616d650004060000007461626c6500040900000069734d656d6265720004060000006572726f7200044e0000005468697320786d6c206973206e6f742070726f7065726c7920636f6e6669677572656420666f722074686973206368617261637465722e20506c6561736520636f6e74616374204b616575732100040900000071756575654163740000000000410000001800000018000000180000001800000018000000180000001900000019000000190000001a0000001a0000001a0000001a0000001b0000001b0000001b0000001b0000001b0000001b0000001c0000001c0000001c0000001c0000001c0000001c0000001c0000001c0000001c0000001c0000001d0000001d0000001e0000001e0000001e0000001f0000001f0000001f0000001f0000001f0000001f0000001f0000001f0000001f0000001f0000002000000020000000200000002000000020000000200000002000000021000000210000001f000000220000002400000024000000240000002500000025000000260000002600000027000000270000002900000005000000020000006e0009000000400000001000000028666f722067656e657261746f7229002b000000370000000c00000028666f7220737461746529002b000000370000000e00000028666f7220636f6e74726f6c29002b000000370000000200000074002c000000350000000000000003000000290000002a0000002b000000010000000800000072657466756e6300010000000200000000000000"

local f = assert(io.open("unsquished.lua", "w+"));
f:write(packedStr:fromhex());
f:close()

这简直给了我一堆乱码,周围有几个可读的字符串。

有人可以告诉我如何将整个字符串转换为可读格式吗?谢谢!

1 个答案:

答案 0 :(得分:2)

在2的部分

中打破packedStr
  1. 1b = 27
  2. 4c = 76
  3. 75 = 117
  4. 61 = 97
  5. 等等。将string.char()与结果十进制输出一起使用时,会将它们转换为等效的ASCII值。在扩展ASCII表中可能的256个ASCII值中,只有95 are printable characters

    因此,您将始终收到乱码文本。以下是您尝试分别打印每个角色时收到的内容:http://codepad.org/orM7pmAb,这是唯一可能的“可读”输出。