tobase2

function tobase2( str ) --> base2str

Description

Encodes a data string to a base 2 string(bitfield).

Parameters

str

The data string that should be encoded.

Return Values

base2str

The bitfield.

Code

--ZFUNC-tobase2-v1
local function tobase2( str ) --> base2str
   return ( str:gsub( '.', function ( c )
               local byte = string.byte( c )
               local bits = {}
               for i = 1,8 do
                  table.insert( bits, byte % 2 )
                  byte = math.floor( byte / 2 )
               end
               return table.concat( bits ):reverse()
            end ) )
end

return tobase2

Examples

local t = require( "taptest" )
local tobase2 = require( "tobase2" )

-- convert data to a bitfields string
t( tobase2( "ACDC" ), "01000001010000110100010001000011" )

t()

See also