Difference between revisions of "Module:Sandbox/Erutuon/UTF-8"

From blackwiki
Jump to navigation Jump to search
blackwiki>Erutuon
(grab binary digit)
blackwiki>Erutuon
(get digit in binary number (or string representation of binary number, though it's kinda silly))
Line 5: Line 5:
 
local rshift = bit.rshift
 
local rshift = bit.rshift
  
 +
-- Converts a string representing a number in binary base to a Lua number.
 
local function binary(stringBinary)
 
local function binary(stringBinary)
 
return tonumber(stringBinary, 2)
 
return tonumber(stringBinary, 2)
Line 11: Line 12:
 
-- Assumes in a single byte, with highest digit first.
 
-- Assumes in a single byte, with highest digit first.
 
local function digitAt(number, index)
 
local function digitAt(number, index)
 +
if type(number) == "string" then
 +
number = binary(number)
 +
end
 
return band(rshift(number, 8 - index), 1)
 
return band(rshift(number, 8 - index), 1)
 
end
 
end
  
 +
-- Returns a table containing bits in a byte, in order.
 
local function getBits(byte)
 
local function getBits(byte)
    local t = {}
+
local t = {}
    for bit = 8, 1, -1 do
+
for bit = 8, 1, -1 do
        t[bit] = band(byte, 1)
+
t[bit] = band(byte, 1)
        byte = rshift(byte, 1)
+
byte = rshift(byte, 1)
    end
+
end
    return t
+
return t
 
end
 
end
  
 
-- mw.log(table.concat(getBits(rshift(binary("11100001"), 8 - 3))))
 
-- mw.log(table.concat(getBits(rshift(binary("11100001"), 8 - 3))))
  
 +
-- Do something to each byte in a string; put the result in a table.
 
local function iterBytes(str, func)
 
local function iterBytes(str, func)
    local out = {}
+
local out = {}
    for i = 1, #str do
+
for i = 1, #str do
        table.insert(out, func(string.byte(str, i)))
+
table.insert(out, func(string.byte(str, i)))
    end
+
end
    return out
+
return out
 +
end
 +
 
 +
local function printBytes(str)
 +
return table.concat(
 +
iterBytes(
 +
str,
 +
function(byte)
 +
return table.concat(getBits(byte))
 +
end
 +
),
 +
" "
 +
)
 
end
 
end
  
 
function p.show(frame)
 
function p.show(frame)
 
local str = frame.args[1] or "ᾰ̓τῑμᾰ́ζω"
 
local str = frame.args[1] or "ᾰ̓τῑμᾰ́ζω"
    return table.concat(
+
return printBytes(str) .. "\n\n" ..
        iterBytes(
+
"digits 3 and 4 in 11100001: " ..
            str,
+
digitAt("11100001", 3) .. ", " .. digitAt("11100001", 4)
            function(byte)
 
                return table.concat(getBits(byte))
 
            end
 
        ),
 
        " "
 
    ) .. "\n\n" .. "digits 3 and 4 in 11100001: " .. digitAt(binary("11100001"), 3) .. ", " .. digitAt(binary("11100001"), 4)
 
 
end
 
end
  
 
return p
 
return p

Revision as of 07:00, 17 September 2017

An experiment with printing binary digit representations of characters.

Usage

01100001 01111110 11000010 10100001 11000011 10100001 11001110 10110001 11100001 10111110 10110000 11110000 10100000 10000000 10000000

digits 3 and 4 in 11100001: 1, 0



local p = {}

local bit = require("bit32")
local band = bit.band
local rshift = bit.rshift

-- Converts a string representing a number in binary base to a Lua number.
local function binary(stringBinary)
	return tonumber(stringBinary, 2)
end

-- Assumes in a single byte, with highest digit first.
local function digitAt(number, index)
	if type(number) == "string" then
		number = binary(number)
	end
	return band(rshift(number, 8 - index), 1)
end

-- Returns a table containing bits in a byte, in order.
local function getBits(byte)
	local t = {}
	for bit = 8, 1, -1 do
		t[bit] = band(byte, 1)
		byte = rshift(byte, 1)
	end
	return t
end

-- mw.log(table.concat(getBits(rshift(binary("11100001"), 8 - 3))))

-- Do something to each byte in a string; put the result in a table.
local function iterBytes(str, func)
	local out = {}
	for i = 1, #str do
		table.insert(out, func(string.byte(str, i)))
	end
	return out
end

local function printBytes(str)
	return table.concat(
		iterBytes(
			str,
			function(byte)
				return table.concat(getBits(byte))
			end
		),
		" "
	)
end

function p.show(frame)
	local str = frame.args[1] or "ᾰ̓τῑμᾰ́ζω"
	return printBytes(str) .. "\n\n" ..
		"digits 3 and 4 in 11100001: " ..
		digitAt("11100001", 3) .. ", " .. digitAt("11100001", 4)
end

return p