--[[---------------
hex conversion lib for lua.
-------------------
part of LuaBit v0.2.
under the MIT lisence.
copyright(c) 2006 hanzhao (abrash_han@hotmail.com)
--]]---------------

require("bit")

local bit = bit
local type = type
local math = math
local tostring = tostring
local table = table
local string = string
local tonumber = tonumber

module("hex")

function to_hex(n)
 if(type(n) ~= "number") then
  error("non-number type passed in.")
 end

 -- checking not float
 if(n - math.floor(n) > 0) then
  error("trying to apply bitwise operation on non-integer!")
 end

 if(n < 0) then
  -- negative
  n = bit.tobits(bit.bnot(math.abs(n)) + 1)
  n = bit.tonumb(n)
 end

 hex_tbl = {'A', 'B', 'C', 'D', 'E', 'F'}
 hex_str = ""

 while(n ~= 0) do
  last = math.fmod(n, 16)
  if(last < 10) then
   hex_str = tostring(last) .. hex_str
  else
   hex_str = hex_tbl[last-10+1] .. hex_str
  end
  n = math.floor(n/16)
 end
 if(hex_str == "") then
  hex_str = "0"
 end
 return "0x" .. hex_str
end

function to_dec(hex)
 if(type(hex) ~= "string") then
  error("non-string type passed in.")
 end

 head = string.sub(hex, 1, 2)
 
 if( head ~= "0x" and head ~= "0X") then
  error("wrong hex format, should lead by 0x or 0X.")
 end

 v = tonumber(string.sub(hex, 3), 16)

 return v;
end

--[[
-- test
d = 4341688
h = to_hex(d)
print(h)
print(to_dec(h))


for i = 1, 100000 do
 h = hex.to_hex(i)
 d = hex.to_dec(h)
 if(d ~= i) then 
  error("failed " .. i .. ", " .. h)
 end
end
--]]