View previous topic :: View next topic |
Author |
Message |
AylinCE Grandmaster Cheater Supreme
Reputation: 36
Joined: 16 Feb 2017 Posts: 1499
|
Posted: Wed Jan 29, 2025 11:52 am Post subject: Compare existing.. (String, Number, Hex (Aobs)) - [Extra] |
|
|
This is an example of coding that parses the class that a string fits.
It has been added to the archive to be found in search results.
Code: | function checkType(input)
local result = ""
if input:match(".*%x+.*") and not input:match("^%s*$") then
if not tonumber(input) then
if not input:match("^%x+$") then
result = "String"
end
else
result = "Number"
return result
end
end
local cleanedInput = input:gsub("%s+", ""):lower()
if cleanedInput:match("^%x+$") then
result = "Hexadecimal"
elseif tonumber(cleanedInput) then
result = "Number"
else
result = "String"
end
return result
end
-- Test:
print(checkType("123")) -- Number
print(checkType("1A3F")) -- Hexadecimal
print(checkType("Hello")) -- String
print(checkType("123ABC")) -- Hexadecimal
print(checkType("123Hello")) -- String
print(checkType("Ed fA BC")) -- Hexadecimal
print(checkType("11 22 37")) -- Hexadecimal
print(checkType("This is a hex code of '11 22 37'.")) -- String
print(checkType("123456")) -- Number
print(checkType("Im of no")) -- String |
If you have a more understandable or detailed example, please feel free to comment.
_________________
|
|
Back to top |
|
 |
daspamer Grandmaster Cheater Supreme
Reputation: 54
Joined: 13 Sep 2011 Posts: 1588
|
Posted: Mon Feb 10, 2025 3:16 pm Post subject: |
|
|
Different approached;
Code: | check = function(p)
if (not p) then return nil end;
local ishex = (p:lower():sub(1,2) == "0x");
if(not ishex and tonumber(p)) then return "number" end;
local hex,changed = p:gsub(" ", "");
if (changed > 0 and tonumber("0x" .. hex)) then return "array of bytes" end;
if ((ishex and tonumber(p)) or tonumber("0x"..hex)) then return "hexadecimal" end;
return "string";
end |
_________________
I'm rusty and getting older, help me re-learn lua.
Last edited by daspamer on Wed Feb 12, 2025 1:32 pm; edited 4 times in total |
|
Back to top |
|
 |
AylinCE Grandmaster Cheater Supreme
Reputation: 36
Joined: 16 Feb 2017 Posts: 1499
|
Posted: Mon Feb 10, 2025 3:36 pm Post subject: |
|
|
Thanks for joining, master.
Code and results: (Returns all as "hexadecimal")
Code: | checkType = function(p)
if (not p) then return nil end;
if(type(p) == 'string' and ('0x'..p or (tonumber(p) and p:lower():sub(1,2) == '0x'))) then return 'hexadecimal' end;
if(tonumber(p)) then return 'number' end;
return 'string';
end
print(checkType("123")) -- hexadecimal
print(checkType("1A3F")) -- hexadecimal
print(checkType("Hello")) -- hexadecimal
print(checkType("123ABC")) -- hexadecimal
print(checkType("123Hello")) -- hexadecimal
print(checkType("Ed fA BC")) -- Hexadecimal
print(checkType("11 22 37")) -- Hexadecimal
print(checkType("This is a hex code of '11 22 37'.")) -- hexadecimal
print(checkType("123456")) -- hexadecimal
print(checkType("Im of no")) -- hexadecimal |
_________________
|
|
Back to top |
|
 |
daspamer Grandmaster Cheater Supreme
Reputation: 54
Joined: 13 Sep 2011 Posts: 1588
|
Posted: Tue Feb 11, 2025 9:24 am Post subject: |
|
|
My bad, order matters and forgot to test if hex was given without prefix.
Edited reply.
_________________
I'm rusty and getting older, help me re-learn lua. |
|
Back to top |
|
 |
AylinCE Grandmaster Cheater Supreme
Reputation: 36
Joined: 16 Feb 2017 Posts: 1499
|
Posted: Tue Feb 11, 2025 10:22 am Post subject: |
|
|
You can test the code with the following queries:
Code: | print(1,check("123")) -- Number
print(2,check("1A3F")) -- Hexadecimal
print(3,check("Hello")) -- String
print(4,check("123ABC")) -- Hexadecimal
print(5,check("123Hello")) -- String
print(6,check("Ed fA BC")) -- Hexadecimal
print(7,check("11 22 37")) -- Hexadecimal
print(8,check("This is a hex code of '11 22 37'.")) -- String
print(9,check("123456")) -- Number
print(10,check("Im of no")) -- String |
You will see that the following two queries are recognized as "String":
Code: | print(check(6,"Ed fA BC")) -- Hexadecimal
print(check(7,"11 22 37")) -- Hexadecimal |
_________________
|
|
Back to top |
|
 |
daspamer Grandmaster Cheater Supreme
Reputation: 54
Joined: 13 Sep 2011 Posts: 1588
|
Posted: Wed Feb 12, 2025 1:35 pm Post subject: |
|
|
Modified reply if you want to differentiate between hexadecimal and array of bytes, though if user to test if his string is array of byte they should know that upfront....
_________________
I'm rusty and getting older, help me re-learn lua. |
|
Back to top |
|
 |
AylinCE Grandmaster Cheater Supreme
Reputation: 36
Joined: 16 Feb 2017 Posts: 1499
|
Posted: Wed Feb 12, 2025 2:42 pm Post subject: |
|
|
This is actually a detail related to the designer, not the user.
In other words, the user can be given a single button, tested for what the entered code (string) represents, and the content of the code that will perform the change can be determined accordingly.
(Otherwise, 3 buttons can be given and the user can choose the change order themselves. Of course, the designer can distinguish this for users who do not understand the situation.)
A usage example is given below;
The wildcard character "??" has been added for the Aobs code.
If both boxes do not have the same order code, the text will be red and the button will be inactive.
Otherwise, everything will proceed smoothly.
Code: | if rplcFrm1 then rplcFrm1.Destroy() rplcFrm1=nil end
DP1=getScreenDPI()/96
rplcFrm1=createForm()
rplcFrm1.height=134*DP1 rplcFrm1.width=567*DP1
rplcFrm1.PopupMode=0 rplcFrm1.caption="String-Double-Aobs Replace!"
rplcFrm1.Position="poDesktopCenter" rplcFrm1.ShowInTaskBar="stAlways"
rplcFrm1.BorderStyle="bsSingle"
-------------------------
local tst = {}
----------------------- tst.lbl1 -----
tst.lbl1=createLabel(rplcFrm1)
tst.lbl1.AutoSize=true
tst.lbl1.height=20*DP1 tst.lbl1.width=50*DP1 tst.lbl1.left=12*DP1 tst.lbl1.top=30*DP1
tst.lbl1.caption="Search:"
tst.lbl1.alignment="taLeftJustify"
tst.lbl1.Font.Style="fsBold" tst.lbl1.Font.Size=11*DP1
-----------------------
----------------------- tst.edt1 -----
tst.edt1=createEdit(rplcFrm1)
tst.edt1.AutoSize=true
tst.edt1.height=23*DP1 tst.edt1.width=420*DP1 tst.edt1.left=72*DP1 tst.edt1.top=27*DP1
tst.edt1.text=""
tst.edt1.alignment="taLeftJustify"
tst.edt1.Font.Style="fsBold" tst.edt1.Font.Size=0*DP1
-----------------------
----------------------- tst.lbl2 -----
tst.lbl2=createLabel(rplcFrm1)
tst.lbl2.AutoSize=true
tst.lbl2.height=20*DP1 tst.lbl2.width=20*DP1 tst.lbl2.left=495*DP1 tst.lbl2.top=30*DP1
tst.lbl2.caption=". . ."
tst.lbl2.alignment="taLeftJustify"
tst.lbl2.Font.Style="fsBold" tst.lbl2.Font.Size=11*DP1
-----------------------
----------------------- tst.lbl3 -----
tst.lbl3=createLabel(rplcFrm1)
tst.lbl3.AutoSize=true
tst.lbl3.height=20*DP1 tst.lbl3.width=58*DP1 tst.lbl3.left=12*DP1 tst.lbl3.top=60*DP1
tst.lbl3.caption="Replace:"
tst.lbl3.alignment="taLeftJustify"
tst.lbl3.Font.Style="fsBold" tst.lbl3.Font.Size=11*DP1
-----------------------
----------------------- tst.edt2 -----
tst.edt2=createEdit(rplcFrm1)
tst.edt2.AutoSize=true
tst.edt2.height=23*DP1 tst.edt2.width=420*DP1 tst.edt2.left=72*DP1 tst.edt2.top=60*DP1
tst.edt2.text=""
tst.edt2.alignment="taLeftJustify"
tst.edt2.Font.Style="fsBold" tst.edt2.Font.Size=0*DP1
-----------------------
----------------------- tst.lbl4 -----
tst.lbl4=createLabel(rplcFrm1)
tst.lbl4.AutoSize=true
tst.lbl4.height=20*DP1 tst.lbl4.width=20*DP1 tst.lbl4.left=495*DP1 tst.lbl4.top=60*DP1
tst.lbl4.caption=". . ."
tst.lbl4.alignment="taLeftJustify"
tst.lbl4.Font.Style="fsBold" tst.lbl4.Font.Size=11*DP1
-----------------------
----------------------- tst.btn1 -----
tst.btn1=createButton(rplcFrm1)
tst.btn1.AutoSize=false
tst.btn1.height=25*DP1 tst.btn1.width=75*DP1 tst.btn1.left=227*DP1 tst.btn1.top=95*DP1
tst.btn1.caption="ENABLE"
tst.btn1.Font.Style="fsBold" tst.btn1.Font.Size=0*DP1
-----------------------
--############################################################################--
--############################################################################--
local chk1 = 1
function checkType(input)
local result = ""
if input:match(".*%x+.*") and not input:match("^%s*$") then
if not tonumber(input) then
if not input:match("^%x+$") then
result = "String"
chk1 = 1
end
else
result = "Double"
chk1 = 2
return result
end
end
local cleanedInput = input:gsub("%s+", ""):lower()
if cleanedInput:match("^%x+$") or cleanedInput:match("^%x+$??") then
result = "Aobs"
chk1 = 3
elseif tonumber(cleanedInput) then
result = "Double"
chk1 = 2
else
result = "String"
chk1 = 1
end
return result
end
-------------------------------------------
function checkEnbl()
ch1 = checkType(tst.edt1.Text)
ch2 = checkType(tst.edt2.Text)
if ch1==ch2 then
tst.edt1.Font.Color = clBlack
tst.edt2.Font.Color = clBlack
tst.btn1.Enabled = true
else
tst.edt1.Font.Color = clRed
tst.edt2.Font.Color = clRed
tst.btn1.Enabled = false
end
end
tst.edt1.OnChange=function(sender)
if sender.Text~="" then
tst.lbl2.caption = checkType(sender.Text)
checkEnbl()
end
end
tst.edt2.OnChange=function(sender)
if sender.Text~="" then
tst.lbl4.caption = checkType(sender.Text)
checkEnbl()
end
end |
_________________
|
|
Back to top |
|
 |
|