Advertisement
gocha

Lua 5.1 basic byte conversion functions

Mar 16th, 2013
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.13 KB | None | 0 0
  1. -- Lua 5.1 basic byte conversion functions
  2.  
  3. local bytestonumber = function(bytestr)
  4.     -- little endian, string.reverse will help to reverse endianness
  5.  
  6.     if bytestr == nil or #bytestr == 0 then
  7.         return nil
  8.     end
  9.  
  10.     local n = 0
  11.     local x = 1
  12.     for i = 1, #bytestr do
  13.         n = n + (bytestr:byte(i) * x)
  14.         x = x * 256
  15.     end
  16.     return n
  17. end;
  18.  
  19. local numbertobytes = function(n, length)
  20.     -- little endian, string.reverse will help to reverse endianness
  21.  
  22.     if n == nil then
  23.         return nil
  24.     end
  25.  
  26.     local bytestr = ""
  27.     -- byte-by-byte conversion
  28.     local x = n
  29.     repeat
  30.         local byteval = x % 256
  31.         if byteval < 0 then
  32.             byteval = 0x100 + x
  33.         end
  34.         bytestr = bytestr .. string.char(byteval)
  35.         x = math.floor(x / 256)
  36.     until not ((n >= 0 and x ~= 0) or (n < 0 and x ~= -1))
  37.     -- length check
  38.     if length and #bytestr > length then
  39.         error("illegal argument #2: out of range, length too short")
  40.     end
  41.     -- add padding bytes
  42.     if length and #bytestr < length then
  43.         bytestr = bytestr .. string.char(n >= 0 and 0 or 255):rep(length - #bytestr)
  44.     end
  45.     return bytestr
  46. end;
  47.  
  48. local hextobytes = function(hexstr)
  49.     if hexstr == nil then
  50.         return nil
  51.     end
  52.  
  53.     if #hexstr % 2 ~= 0 then
  54.         error("illegal argument #1")
  55.     end
  56.  
  57.     local bytestr = ""
  58.     for i = 1, #hexstr, 2 do
  59.         local s = hexstr:sub(i, i + 1)
  60.         local n = tonumber(s, 16)
  61.         if n == nil then
  62.             error("illegal argument #1")
  63.         end
  64.         bytestr = bytestr .. string.char(n)
  65.     end
  66.     return bytestr
  67. end;
  68.  
  69. local bytestohex = function(bytestr)
  70.     if bytestr == nil then
  71.         return nil
  72.     end
  73.  
  74.     local hexstr = ""
  75.     for i = 1, #bytestr do
  76.         hexstr = hexstr .. string.format("%02x", bytestr:byte(i))
  77.     end
  78.     return hexstr
  79. end;
  80.  
  81. local bytestoarray = function(bytestr)
  82.     if bytestr == nil then
  83.         return nil
  84.     end
  85.  
  86.     local bytearray = {}
  87.     for i = 1, #bytestr do
  88.         bytearray[i] = bytestr:byte(i)
  89.     end
  90.     return bytearray
  91. end;
  92.  
  93. local arraytobytes = function(bytearray)
  94.     -- equal to string.char(unpack(bytearray)), but this one uses less stack
  95.  
  96.     if bytearray == nil then
  97.         return nil
  98.     end
  99.  
  100.     local bytestr = ""
  101.     for i, v in ipairs(bytearray) do
  102.         bytestr = bytestr .. string.char(v)
  103.     end
  104.     return bytestr
  105. end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement