Lua 언어 소스:
-- Filename: testHexView_02.lua
--
-- Execute: lua testHexView_02.lua [filename]
--
-- Date: 2013. 7. 30.
--
-- See: http://java.about.com/od/InputOutput/ss/Binary-Stream-Example-Code.htm
function printUsage()
print "Usage: lua testHexView_02.lua [filename]"
end
function toHex(b)
local bit = require("bit")
local s = ""
local x1 = bit.rshift(bit.band(b, 0xF0), 4)
local x2 = bit.band(b, 0x0F)
if x1 < 10 then
s = s .. string.char(x1 + string.byte("0"))
else
s = s .. string.char((x1 - 10) + string.byte("A"))
end
if x2 < 10 then
s = s .. string.char(x2 + string.byte("0"))
else
s = s .. string.char((x2 - 10) + string.byte("A"))
end
return s
end
function toHex8(n)
local bit = require("bit")
local s = ""
local x1 = bit.rshift(bit.band(n, 0xF0000000), 28)
local x2 = bit.rshift(bit.band(n, 0xF000000), 24)
local x3 = bit.rshift(bit.band(n, 0xF00000), 20)
local x4 = bit.rshift(bit.band(n, 0xF0000), 16)
local x5 = bit.rshift(bit.band(n, 0xF000), 12)
local x6 = bit.rshift(bit.band(n, 0xF00), 8)
local x7 = bit.rshift(bit.band(n, 0xF0), 4)
local x8 = bit.band(n, 0xF)
if x1 < 10 then
s = s .. string.char(x1 + string.byte("0"))
else
s = s .. string.char((x1 - 10) + string.byte("A"))
end
if x2 < 10 then
s = s .. string.char(x2 + string.byte("0"))
else
s = s .. string.char((x2 - 10) + string.byte("A"))
end
if x3 < 10 then
s = s .. string.char(x3 + string.byte("0"))
else
s = s .. string.char((x3 - 10) + string.byte("A"))
end
if x4 < 10 then
s = s .. string.char(x4 + string.byte("0"))
else
s = s .. string.char((x4 - 10) + string.byte("A"))
end
s = s .. " "
if x5 < 10 then
s = s .. string.char(x5 + string.byte("0"))
else
s = s .. string.char((x5 - 10) + string.byte("A"))
end
if x6 < 10 then
s = s .. string.char(x6 + string.byte("0"))
else
s = s .. string.char((x6 - 10) + string.byte("A"))
end
if x7 < 10 then
s = s .. string.char(x7 + string.byte("0"))
else
s = s .. string.char((x7 - 10) + string.byte("A"))
end
if x8 < 10 then
s = s .. string.char(x8 + string.byte("0"))
else
s = s .. string.char((x8 - 10) + string.byte("A"))
end
return s
end
local bit = require("bit")
function fsize (file)
local current = file:seek() -- get current position
local size = file:seek("end") -- get file size
file:seek("set", current) -- restore position
return size
end
if #arg < 1 then
printUsage()
os.exit( 1 )
end
local fname = arg[1]
local file2, err = io.open(fname, "rb") -- open a binary file
if err then
print( err )
os.exit( 1 )
end
local fsize1 = fsize(file2)
print( "The size of the file \"" .. fname .. " is " .. fsize1 .. ".")
print()
local a
local dum = ""
local n = 0
if file2 then
dum = ""
while n < fsize1 do
if n % 16 == 0 then
io.write( toHex8( bit.band(n, 0xFFFFFFFF) ) .. ": " )
end
if not (n % 16 == 8) then
io.write(" ")
else
io.write("-")
end
a = file2:read(1):byte(1, 1)
io.write( toHex(a) )
if a < string.byte(" ") or a > 0x7F then
dum = dum .. "."
else
dum = dum .. string.char(a)
end
n = n + 1
if n % 16 == 0 then
io.write(" |" .. dum .. "|\n")
dum = ""
end
end
if dum ~= "" then
for k = 1,16-#dum do
io.write(" ")
end
io.write(" |" .. dum)
for k = 1,16-#dum do
io.write(" ")
end
io.write("|\n")
dum = ""
end
file2:close()
print()
print()
print("Read " .. n .. " bytes")
end
실행하기 예 1> lua testHexView_02.lua temp_1.bin
The size of the file "temp_1.bin is 12.
0000 0000: 48 65 6C 6C 6F 20 74 68-65 72 65 0A |Hello there. |
Read 12 bytes
실행하기 예 2> lua testHexView_02.lua myFile.ser
The size of the file "myFile.ser is 130.
0000 0000: AC ED 00 05 73 72 00 06-50 65 72 73 6F 6E 07 31 |....sr..Person.1|
0000 0010: 46 DB A5 1D 44 AB 02 00-03 49 00 03 61 67 65 4C |F...D....I..ageL|
0000 0020: 00 09 66 69 72 73 74 4E-61 6D 65 74 00 12 4C 6A |..firstNamet..Lj|
0000 0030: 61 76 61 2F 6C 61 6E 67-2F 53 74 72 69 6E 67 3B |ava/lang/String;|
0000 0040: 4C 00 08 6C 61 73 74 4E-61 6D 65 71 00 7E 00 01 |L..lastNameq.~..|
0000 0050: 78 70 00 00 00 13 74 00-05 4A 61 6D 65 73 74 00 |xp....t..Jamest.|
0000 0060: 04 52 79 61 6E 73 71 00-7E 00 00 00 00 00 1E 74 |.Ryansq.~......t|
0000 0070: 00 07 4F 62 69 2D 77 61-6E 74 00 06 4B 65 6E 6F |..Obi-want..Keno|
0000 0080: 62 69 |bi |
Read 130 bytes
'프로그래밍 > Lua' 카테고리의 다른 글
Lua 언어로 역삼각함수, 역쌍곡선함수 값을 구하는 예제 (0) | 2013.01.01 |
---|---|
스트링 리스트에서 스트링 찾기(find) with Lua (0) | 2009.04.22 |
스트링 배열 정렬(sorting)하기 with Lua (0) | 2009.04.20 |
손으로 계산하는 긴자리 곱셈표 만들기 with Lua (0) | 2009.03.06 |
손으로 만드는 나눗셈 계산표 with Lua (0) | 2008.05.16 |