Last active
December 15, 2015 07:59
-
-
Save turbidsoul/5226985 to your computer and use it in GitHub Desktop.
lua 16进制编码
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local str = "2e7ac38c09ddc025825be6267fd11725" | |
hex = {} | |
local HexToDec={ | |
['0'] = 0, ['1'] = 1, ['2'] = 2, ['3'] = 3, | |
['4'] = 4, ['5'] = 5, ['6'] = 6, ['7'] = 7, | |
['8'] = 8, ['9'] = 9, ['A'] = 10, ['B'] = 11, | |
['C'] = 12, ['D'] = 13, ['E'] = 14, ['F'] = 15, | |
['A'] = 10, ['B'] = 11, ['C'] = 12, ['D'] = 13, | |
['E'] = 14, ['F'] = 15 | |
} | |
local DecToHex='0123456789ABCDEF' | |
local function char(S, i) | |
print(S:sub(i,i)) | |
local B1 = HexToDec[S:sub(i,i)] | |
local B2 = HexToDec[S:sub(i+1,i+1)] | |
local C = B1 *16 + B2 | |
return C | |
end | |
function dec(S) | |
local D = S:gsub("%s", '') | |
D = D:gsub("\n", '') | |
local T = {} | |
local i = 1 | |
local c = 1 | |
while (i < #D ) do | |
local C = char(D,i) | |
i = i + 2 | |
T[c] = string.char(C) | |
c = c + 1 | |
end | |
return table.concat(T) | |
end | |
function enc(S) | |
local T = {} | |
local j = 1 | |
local B | |
for i=1, #S do | |
B = S:byte(i) / 16 + 1 | |
T[j] = DecToHex:sub(B,B) | |
B = S:byte(i) % 16 + 1 | |
T[j+1] = DecToHex:sub(B,B) | |
j = j + 2 | |
end | |
return table.concat(T) | |
end | |
print(hex.enc(hex.dec(string.upper(str)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment