From 5124189b4e2c50c5f23fea615181b6350713e583 Mon Sep 17 00:00:00 2001 From: Jon Michael Aanes Date: Mon, 1 May 2017 16:23:57 +0200 Subject: [PATCH] Added `to_bin` function. --- cdata.lua | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/cdata.lua b/cdata.lua index f4013f7..acfc961 100644 --- a/cdata.lua +++ b/cdata.lua @@ -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