Working with Databases

Dealing with binary data

Binary data is currently supported for SQL server ODBC connections.

Unique identifier

UUIDs are read from the database as binary data. The following code can be used to convert the binary data into a readable representation:

function binToUUID (Bin)
   -- Byte ordering for endian conversion
   local ByteOrdering = { 4, 3, 2, 1, 0, 6, 5, 0, 8, 7, 0, 9, 10, 0, 11, 12, 13, 14, 15, 16 }

   local OutText = ""
   for i = 1, #ByteOrdering do
      local bo = ByteOrdering[i]
      if bo == 0 then
         OutText = OutText..'-'
      else
         OutText = OutText .. string.format ("%02X", string.byte(Bin,bo))
      end
   end
   return OutText   
end

ASCII hex display

The following snippet can be used to convert binary data into a human-readable hex based ASCII:

function binToHex (Bin, RowWidth)
   local OutText = ""
   for i = 1, #Bin - 1 do
      if (i > 1 and RowWidth and (i-1) % RowWidth == 0) then
         OutText = OutText .. '\n'
      end
      OutText = OutText .. string.format ("%02X", string.byte(Bin,i)) .. ' '
   end
   OutText = OutText .. string.format ("%02X", string.byte(Bin,#Bin))
   return OutText  
end

Leave A Comment?

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.