the title might look very stupid but I really can't find out what the problem is, I have created a custom function to substring like the way in C++ because I like it:
local function SubStr(String, Start, Count)
return String:sub(Start, Start+(Count-1))
end
This is the code I am using, the substring function seems to work for the Bit Depth, Width and height, though when reading the colour values, it seems to offset a ton and I'm not sure what it is:
print("BitDepth", SubStr(Text, 1, 8))
print("Width", SubStr(Text, 9, 8))
print("Height", SubStr(Text, 17, 8))
Image["BitDepth"] = tonumber(SubStr(Text, 1, 8), 2)
Image["Width"] = tonumber(SubStr(Text, 9, 8), 2)
Image["Height"] = tonumber(SubStr(Text, 17, 8), 2)
for i = 1, Image["BitDepth"] do
Image["Colours"] = {};
Image["Colours"][i] = {};
print("RED", SubStr(Text, (24*i), 8))
print("BLUE", SubStr(Text, (24*i)+8, 8))
print("GREEN", SubStr(Text, (24*i)+16, 8))
print("ALPHA", SubStr(Text, (24*i)+24, 8))
The intended binary colour values for when i = 1, should all be 11111111
By the way this is reading the following binary string:
000000110000100000001000111111111111111111111111000000000000000000000000111111110000000000000000000000000000000011111111
I made some changes; test this out. The first offset start at 25 and second offset start from multiples of 32.
-- New v**strong text**ariables
offset = 25
count = 0; -- count is essentially (i - 1)
for i = 1, Image["BitDepth"] do
Image["Colours"] = {};
Image["Colours"][i] = {};
print(i)
-- first_offset + second_offset + third_offset
print("RED", SubStr(Text, offset + (32*count), 8))
print("BLUE", SubStr(Text, offset + (32*count)+8, 8))
print("GREEN", SubStr(Text, offset + (32*count)+16, 8))
print("ALPHA", SubStr(Text, offset + (32*count)+24, 8))
count = count + 1
end
3x8 is 24. So the next value starts at 25, not 24.
Related
Having some Lua trouble with a a modification of Fisher-Yates shuffle in place. For example, let's say I have a 16 item table (sequence). I want to shuffle integers 1-4 then apply the shuffled pattern in the table to 1-4, 5-8, 9-12, 13-16. So:
{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }
with a 4 item shuffling pattern of 4,2,3,1 would become:
{ 4, 2, 3, 1, 8, 6, 7, 5, 12, 10, 11, 9, 16, 14, 15, 13 }
The code here is from context and includes the "rising edge" input I am using to reshuffle. If you look at the test pic below you can see that yes, it shuffles each section in place, but it reshuffles each section -- I want the shuffled pattern to repeat.
t = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}
range = 4
local function ShuffleInPlace(t)
for i = #t, 2, -1 do
local j = math.random(1, range)
local k = (math.floor(i/(range+.001)))*range + j
t[i], t[j] = t[j], t[i]
end
end
-- initialize new table for shuffling
if s == nil then s = {} end
-- use gate rising edge to shuffle
if prev == nil then prev = 0 end
if gate > 0 and prev <= 0 then
s = t
ShuffleInPlace(s)
end
prev = gate
Test pic:
LMD, thank you, your helpful reply is uncovering a solution (by creating the shuffled "pattern" sequence first, outside the iterator). (Still some issues with the first value I'm working out. And I might be looking at some biproducts of the not-so-great math.random function, but that's another story). I'm a novice so any suggestions are appreciated!
-- range input is 0 to 1
seqRange = math.floor(range*(#t*.99))
local function ShuffleRange(x)
if rdm == nil then rdm = {} end
for m = 1, x do rdm[m] = m end
for m = #rdm, 2, -1 do
local j = math.random(m)
rdm[m], rdm[j] = rdm[j], rdm[m]
return rdm[m]
end
end
local function ShuffleInPlace(t)
y = ShuffleRange(seqRange)
for i = #t, 2, -1 do
local j = (math.floor(i/(seqRange*1.001)))*seqRange + y
t[i], t[j] = t[j], t[i]
end
end
Here's how I would do it, implementing the simple approach of first generating a series of swaps and then applying that to the sublists of length n:
math.randomseed(os.time()) -- seed the random
local t = {}; for i = 1, 16 do t[i] = i end -- build table
local n = 4 -- size of subtables
local swaps = {} -- list of swaps of offsets (0-based)
for i = 0, n - 1 do
-- Insert swap into list of swaps to carry out
local j = math.random(i, n - 1)
table.insert(swaps, {i, j})
end
-- Apply swaps to every subtable from i to i + n
for i = 1, #t, n do
for _, swap in ipairs(swaps) do
-- Swap: First add offsets swap[1] & swap[2] respectively
local a, b = i + swap[1], i + swap[2]
t[a], t[b] = t[b], t[a]
end
end
print(table.concat(t, ", "))
Example output: 4, 2, 1, 3, 8, 6, 5, 7, 12, 10, 9, 11, 16, 14, 13, 15
The formula in question is =IF (ISBLANK(H2),"", ARRAY_CONSTRAIN(ARRAYFORMULA(IF( (MOD(SUM(INT(MID(REPT("0",20-LEN(H2))&H2,ROW($1:$31),1)*(MOD(ROW($1:$31),2)+1)/10)+MOD(MID(REPT("0",20-LEN(H2))&H2,ROW($1:$31),1)*(MOD(ROW($1:$31),2)+1),10)),10)=0), "✔", "❌")), 1, 1))
In English it checks if H2 contains a valid credit card (passing Luhn's algorithm, discussion / sample data here). The expected output is valid = ✔; invalid = ❌; if blank then nothing.
I'm trying to adjust this to appear in every row, but can't seem to nail it down. (Using the trick like for a formula =LEFT(H2,4)&" "&MID(H2,5,6), if it's =arrayformula(LEFT(H2:H100,4)&" "&MID(H2:H100,5,6)) it appears in every row without having to manually refill it when a new row is inserted).
Sample google sheet.
Try this:
=ARRAYFORMULA(
IF(
H2:H = "",
"",
IF(
MOD(
MMULT(
MID(REPT("0", 20 - LEN(H2:H)) & H2:H, SEQUENCE(1, 10, 2, 2), 1)
+ MID(REPT("0", 20 - LEN(H2:H)) & H2:H, SEQUENCE(1, 10, 1, 2), 1) * 2
- (MID(REPT("0", 20 - LEN(H2:H)) & H2:H, SEQUENCE(1, 10, 1, 2), 1) * 2 > 9) * 9,
SEQUENCE(10, 1, 1, 0)
),
10
) = 0,
"✔",
"❌"
)
)
)
If you want a more general solution (for card numbers longer than 20 digits), replace 20 with MAX(LEN(H2:H)) + MOD(MAX(LEN(H2:H)), 2), and 10 with (MAX(LEN(H2:H)) + MOD(MAX(LEN(H2:H)), 2)) / 2.
So I am fairly new to lua and I am learning how to use it by making a checkers app for android. When I found out that I would have to make the circles myself without a function i decided to look for other ways and it looks like the drawPoint() function is the best answer. When I made an equation to try and test it I got a error with the draw point function. It says:
attempt to call global 'drawPoint'(a nil value)
stack traceback:
main.lua54: in main chuck
am I missing something like do I have to import a library to use it? Thank you in advance and just for more background I was giving it the parameter of 2 ints. Here is my whole code below
--All the essential values
local widget = require ("widget")
local redCount = 12
local blackCount = 12
local length = 40.05
local x = length / 2
local y = 80
local startX = x
local startY = y
local allowMoves = true
--Title Display
local title = display.newText("Checkers", display.contentCenterX, 10, native.systemFontBold, 40)
--Functions
local function checkWinner()
if(redCount == 0) then
display.newText("Black Team Wins!!!", display.contentCenterX, display.contentHeight - 60, nativeSystemFontBold, 37)
elseif(blackCount == 0) then
display.newText("Red Team Wins!!!", display.contentCenterX, display.contentHeight - 60, nativeSystemFontBold, 40)
end
end
--Making the board
for i = 1, 8, 1
do
for k = 1, 4, 1
do
if i % 2 == 1 then
display.newRect( x, y, length, length, 30)
else
display.newRect( x + length, y, length, length, 30)
end
x = x + (startX * 4)
end
y = y + length
x = startX
end
--Making the outline
display.newRect(startX, startY - (length / 2), length * 15, 3, 30)
display.newRect(startX, y - (length / 2), length * 15, 3, 30)
display.newRect(startX - (length / 2), startY, 3, length * 15, 30)
display.newRect(320.4, startY, 3, length * 15, 30)
--Making the actual peices
drawPoint(2,5)
While reading some CTF write-ups I came across this script
#!/usr/bin/env python
import struct
import Image
import dpkt
INIT_X, INIT_Y = 100, 400
def print_map(pcap, device):
picture = Image.new("RGB", (1200, 500), "white")
pixels = picture.load()
x, y = INIT_X, INIT_Y
for ts, buf in pcap:
device_id, = struct.unpack("b", buf[0x0B])
if device_id != device:
continue
data = struct.unpack("bbbb", buf[-4:])
status = data[0]
x = x + data[1]
y = y + data[2]
if (status == 1):
for i in range(-5, 5):
for j in range(-5, 5):
pixels[x + i , y + j] = (0, 0, 0, 0)
else:
pixels[x, y] = (255, 0, 0, 0)
picture.save("riverside-map.png", "PNG")
if __name__ == "__main__":
f = open("usb.pcap", "rb")
pcap = dpkt.pcap.Reader(f)
print_map(pcap, 5)
f.close()
And when I run it on my usb.pcap I get this error:
Traceback (most recent call last):
File "test.py", line 39, in <module>
print_map(pcap, n)
File "test.py", line 31, in print_map
pixels[x, y] = (255, 0, 0, 0)
IndexError: image index out of range
Why it is happening?
Depending on the dataset in your usb.pcap file, you may need to adjust the INIT_X and INIT_Y variables. The problem is that struct.unpack returns a signed value, so if the data is over 127 then it appears negative and you are exceeding the array boundaries. If the data is really always positive, you can test for that and force it to a positive value. Something like:
data = [item + 256 if item < 0 else item for item in data]
As Steve Cohen noticed your data is unsigned byte in range -128...127 but if these are indexes of the array than most probably they should be unsigned.
Python's struct has format characters for most cases, use the right ones:
data = struct.unpack("BBBB", buf[-4:]) # tuple of four unsigned bytes
I have the following binary clock that I grabbed from this wiki article (the one that's for v1.5.*) for the awesome WM:
binClock = wibox.widget.base.make_widget()
binClock.radius = 1.5
binClock.shift = 1.8
binClock.farShift = 2
binClock.border = 1
binClock.lineWidth = 1
binClock.colorActive = beautiful.bg_focus
binClock.fit = function(binClock, width, height)
local size = math.min(width, height)
return 6 * 2 * binClock.radius + 5 * binClock.shift + 2 * binClock.farShift + 2 * binClock.border + 2 * binClock.border, size
end
binClock.draw = function(binClock, wibox, cr, width, height)
local curTime = os.date("*t")
local column = {}
table.insert(column, string.format("%04d", binClock:dec_bin(string.sub(string.format("%02d", curTime.hour), 1, 1))))
table.insert(column, string.format("%04d", binClock:dec_bin(string.sub(string.format("%02d", curTime.hour), 2, 2))))
table.insert(column, string.format("%04d", binClock:dec_bin(string.sub(string.format("%02d", curTime.min), 1, 1))))
table.insert(column, string.format("%04d", binClock:dec_bin(string.sub(string.format("%02d", curTime.min), 2, 2))))
table.insert(column, string.format("%04d", binClock:dec_bin(string.sub(string.format("%02d", curTime.sec), 1, 1))))
table.insert(column, string.format("%04d", binClock:dec_bin(string.sub(string.format("%02d", curTime.sec), 2, 2))))
local bigColumn = 0
for i = 0, 5 do
if math.floor(i / 2) > bigColumn then
bigColumn = bigColumn + 1
end
for j = 0, 3 do
if string.sub(column[i + 1], j + 1, j + 1) == "0" then
active = false
else
active = true
end
binClock:draw_point(cr, bigColumn, i, j, active)
end
end
end
binClock.dec_bin = function(binClock, inNum)
inNum = tonumber(inNum)
local base, enum, outNum, rem = 2, "01", "", 0
while inNum > (base - 1) do
inNum, rem = math.floor(inNum / base), math.fmod(inNum, base)
outNum = string.sub(enum, rem + 1, rem + 1) .. outNum
end
outNum = inNum .. outNum
return outNum
end
binClock.draw_point = function(binClock, cr, bigColumn, column, row, active)
cr:arc(binClock.border + column * (2 * binClock.radius + binClock.shift) + bigColumn * binClock.farShift + binClock.radius,
binClock.border + row * (2 * binClock.radius + binClock.shift) + binClock.radius, 2, 0, 2 * math.pi)
if active then
cr:set_source_rgba(0, 0.5, 0, 1)
else
cr:set_source_rgba(0.5, 0.5, 0.5, 1)
end
cr:fill()
end
binClocktimer = timer { timeout = 1 }
binClocktimer:connect_signal("timeout", function() binClock:emit_signal("widget::updated") end)
binClocktimer:start()
First, if something isn't by default already in Lua that's because this is to be used in the config file for awesome. :)
OK, so what I need is some guidance actually. I am not very familiar with Lua currently, so some guidance is all I ask so I can learn. :)
OK, so first, this code outputs a normal binary clock, but every column has 4 dots (44,44,44), instead of a 23,34,34 setup for the dots, as it would be in a normal binary clock. What's controlling that in this code? So that I can pay around with it.
Next, what controls the color? Right now it's gray background and quite a dark green, I want to brighten both of those up.
And what controls the smoothing? Right now it's outputting circles, would like to see what it's like for it to output squares instead.
That's all I need help with, if you can point me to the code and some documentation for what I need, that should be more than enough. :)
Also, if somebody would be nice enough to add some comments, that also would be awesome. Don't have to be very detailed comments, but at least to the point where it gives an idea of what each thing does. :)
EDIT:
Found what modifies the colors, so figured that out. None of the first variables control if it's a square or circle BTW. :)
The draw_point function draws the dots.
The two loops in the draw function are what create the output and is where the columns come from. To do a 23/34/34 layout you would need to modify the inner loop skip the first X points based on the counter of the outer loop I believe.