1
0

Added to_bin function.

This commit is contained in:
Jon Michael Aanes 2017-05-01 16:23:57 +02:00
parent df232b144b
commit 5124189b4e

View File

@ -9,10 +9,11 @@ local bit = require 'bit'
--------------------------------------------------------------------------------
-- Util
local NUMBER_TO_HEX = {
[00] = '0', [01] = '1', [02] = '2', [03] = '3', [04] = '4', [05] = '5',
[06] = '6', [07] = '7', [08] = '8', [09] = '9', [10] = 'A', [11] = 'B',
[12] = 'C', [13] = 'D', [14] = 'E', [15] = 'F',
local HEX_TO_BIN = {
['0'] = '0000', ['1'] = '0001', ['2'] = '0010', ['3'] = '0011',
['4'] = '0100', ['5'] = '0101', ['6'] = '0110', ['7'] = '0111',
['8'] = '1000', ['9'] = '1001', ['A'] = '1010', ['B'] = '1011',
['C'] = '1100', ['D'] = '1101', ['E'] = '1110', ['F'] = '1111',
}
local function to_hex (val, nr_elements, element_size)
@ -26,6 +27,10 @@ local function to_hex (val, nr_elements, element_size)
return table.concat(l, '')
end
local function to_bin (val, nr_elements, element_size)
return to_hex(val, nr_elements, element_size):gsub('[0-9A-F]', HEX_TO_BIN)
end
local function is_nice_unicode_string (str)
-- TODO... Maybe also look into a purely binary oriented representation.
return false
@ -104,6 +109,7 @@ local function format_cdata (value, options, depth, l, format_value)
if nr_layers == 1 then
-- Only a single level of arrays
l[#l+1] = '\n\thex = ' .. to_hex(value, nr_elements, element_size) .. ','
l[#l+1] = '\n\tbin = ' .. to_bin(value, nr_elements, element_size) .. ','
end
end