-- Converts a byte to a string of 0s and 1s.
function byte2bin(n)
local t = {}
for i=7,0,-1 do
t[#t+1] = math.floor(n / 2^i)
n = n % 2^i
end
return table.concat(t)
end
a = byte2bin(127)
print(a)
输出:
01111111
原文 :
本文介绍了一个Lua函数,用于将字节转换为二进制字符串表示。通过逐位处理,该函数能准确地将输入的字节转换为对应的二进制形式,如将127转换为'01111111'。
-- Converts a byte to a string of 0s and 1s.
function byte2bin(n)
local t = {}
for i=7,0,-1 do
t[#t+1] = math.floor(n / 2^i)
n = n % 2^i
end
return table.concat(t)
end
a = byte2bin(127)
print(a)
输出:
01111111
原文 :
6348

被折叠的 条评论
为什么被折叠?