The Boston Diaries

The ongoing saga of a programmer who doesn't live in Boston, nor does he even like Boston, but yet named his weblog/journal “The Boston Diaries.”

Go figure.

Wednesday, January 25, 2012

99 ways to program a hex, Part 17: Lua, recursion, runtime type checking

Since Lua is a dynamically typed language (“values have types, not variables”) we can check the type of a variable at runtime and behave accordingly. Before, we were restricted with just dumping a file, but we could also dump strings (which in Lua can be pure binary data). So today's version checks what type the input is; if it's a file, we read data from there, otherwise if the input is a string, we pull the next blob of data out of it.

Granted, we don't actually use that feature here, but we can more easily reuse do_dump() elsewhere.

#!/usr/bin/env lua
-- ***************************************************************
-- Copyright 2010 by Sean Conner.  All Rights Reserved.
-- This program is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- (at your option) any later version.
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- GNU General Public License for more details.
-- You should have received a copy of the GNU General Public License
-- along with this program.  If not, see <>.
-- Comments, questions and criticisms can be sent to:
-- ********************************************************************

-- Style: Lua 5.1, recursion, runtime type checking

function do_dump(fpin,fpout,offset)
  local line
  if type(fpin) == 'string' then
    if offset > string.len(fpin) then return end
    line = fpin:sub(offset + 1,offset + 16)
    line = fpin:read(16)
    if line == nil then return end

  	string.format("%08X: ",offset),
  	line:gsub(".",function(c) return string.format("%02X ",c:byte()) end),
  	string.rep(" ",3 * (16 - line:len())),
  return do_dump(fpin,fpout,offset + 16)

-- **********************************************************************

if #arg == 0 then
  for i = 1 , #arg do
    local f =[1],"r")


Obligatory Picture

[It's the most wonderful time of the year!]

Obligatory Links

Obligatory Miscellaneous

You have my permission to link freely to any entry here. Go ahead, I won't bite. I promise.

The dates are the permanent links to that day's entries (or entry, if there is only one entry). The titles are the permanent links to that entry only. The format for the links are simple: Start with the base link for this site:, then add the date you are interested in, say 2000/08/01, so that would make the final URL:

You can also specify the entire month by leaving off the day portion. You can even select an arbitrary portion of time.

You may also note subtle shading of the links and that's intentional: the “closer” the link is (relative to the page) the “brighter” it appears. It's an experiment in using color shading to denote the distance a link is from here. If you don't notice it, don't worry; it's not all that important.

It is assumed that every brand name, slogan, corporate name, symbol, design element, et cetera mentioned in these pages is a protected and/or trademarked entity, the sole property of its owner(s), and acknowledgement of this status is implied.

Copyright © 1999-2020 by Sean Conner. All Rights Reserved.