Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


Lua on bit32.. (@DB, @atom0s)

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting
View previous topic :: View next topic  
Author Message
AylinCE
Grandmaster Cheater Supreme
Reputation: 37

Joined: 16 Feb 2017
Posts: 1516

PostPosted: Fri Apr 18, 2025 6:00 am    Post subject: Lua on bit32.. (@DB, @atom0s) Reply with quote

I see that "bit32" is marked as the syntax in CE.

But when I want to use it, it gives "undefined, nil".
Instead, I had to define "bit32" for "XOR".

Probably because "bit32" was deprecated after Lua 5.2 and a 64 bit version was defined. (5.2+)

My question: Which one do you find more useful?

Of course, the intended use is also important.
Here is a standalone example:

Code:
-- Defining bit32 XOR function manually
local bit32 = {}

function bit32.bxor(a, b)
    local result = 0
    local bit = 1
    while a > 0 or b > 0 do
        local a_bit = a % 2
        local b_bit = b % 2
        local xor_bit = (a_bit + b_bit) % 2
        result = result + xor_bit * bit
        a = math.floor(a / 2)
        b = math.floor(b / 2)
        bit = bit * 2
    end
    return result
end

-- XOR Encryption Function
function xorEncryptDecrypt(input, key)
    local output = {}
    local out2 = ""
    for i = 1, #input do
        local char = string.byte(input, i)
        local encryptedChar = bit32.bxor(char, key)
        table.insert(output, string.char(encryptedChar))
    end
    out2 = table.concat(output)
    out2 = string.gsub(out2, "[\n\r]", " ")
    return out2
end

-- Encryption Key
local key = 42

-- Text to be Encrypted
local originalText = "print('This is a test text.')"

-- Encode
local encryptedText = xorEncryptDecrypt(originalText, key)
print("Encode Text:")
print(encryptedText)
-- Ahh.. It would be better to write the output to a ".lua" file. ;)

-- Decode
local decryptedText = xorEncryptDecrypt(encryptedText, key)
print("Decode Text:")
print(decryptedText)


Or "64-bit" example:

Code:
-- 64-bit values
local a = 0x12345678ABCDEF01  -- A 64-bit number
local b = 0xF0F0F0F0F0F0F0F0  -- Another 64-bit number

-- Bitwise "AND" operation
local and_result = a & b
print(string.format("AND result: 0x%X", and_result))

-- Bitwise "OR" operation
local or_result = a | b
print(string.format("OR result: 0x%X", or_result))

-- Bitwise "XOR" operation
local xor_result = a ~ b
print(string.format("XOR result: 0x%X", xor_result))

-- Bitwise shifts (left and right)
local shift_left = a << 4  -- Shift left by 4 bits
local shift_right = b >> 4 -- Shift right by 4 bits
print(string.format("Left shift: 0x%X", shift_left))
print(string.format("Right shift: 0x%X", shift_right))


Results:
Code:
AND result: 0x10305070A0C0E000
OR result: 0xF2F4F6F8FBFDFFF1
XOR result: 0xE2C4A6885B3D1FF1
Left shift: 0x2345678ABCDEF010
Right shift: 0xF0F0F0F0F0F0F0F


On the other hand, I can see that the required operation works without using "bit32":
Code:
function xorEncryptDecrypt(input, key)
    local output = {}
    for i = 1, #input do
        local char = string.byte(input, i)
        local encryptedChar = char ~ key
        table.insert(output, string.char(encryptedChar))
    end
    local singleLineOutput = table.concat(output)
    singleLineOutput = string.gsub(singleLineOutput, "[\n\r]", " ")
    return singleLineOutput
end


"bit32" is not defined in "celua.txt" but it is still marked in Lua Script.
Why?

_________________
Hi Hitler Different Trainer forms for you!
https://forum.cheatengine.org/viewtopic.php?t=619279
Enthusiastic people: Always one step ahead
Do not underestimate me Master: You were a beginner in the past
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
ParkourPenguin
I post too much
Reputation: 152

Joined: 06 Jul 2014
Posts: 4695

PostPosted: Fri Apr 18, 2025 11:04 am    Post subject: Reply with quote

Use Lua's builtin binary ops. e.g. xor: `~`

AylinCE wrote:
"bit32" is not defined in "celua.txt" but it is still marked in Lua Script.
Why?
Lua 5.2's bit32 stdlib was deprecated in favor of Lua 5.3's new binary ops.
It still gets highlighted because DB didn't remove the code that highlighted "bit32". I don't think this has anything to do with backwards compatibility... perhaps it was simply forgotten.

_________________
I don't know where I'm going, but I'll figure it out when I get there.
Back to top
View user's profile Send private message
AylinCE
Grandmaster Cheater Supreme
Reputation: 37

Joined: 16 Feb 2017
Posts: 1516

PostPosted: Fri Apr 18, 2025 1:43 pm    Post subject: Reply with quote

ParkourPenguin wrote:
...


Probably as you said.
I am evaluating applications to shorten some codes.

Actually, he may have forgotten the emphasis because there is no need for "bit32".

I am just wondering if there are other options. Smile

_________________
Hi Hitler Different Trainer forms for you!
https://forum.cheatengine.org/viewtopic.php?t=619279
Enthusiastic people: Always one step ahead
Do not underestimate me Master: You were a beginner in the past
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
atom0s
Moderator
Reputation: 205

Joined: 25 Jan 2006
Posts: 8585
Location: 127.0.0.1

PostPosted: Fri Apr 18, 2025 10:14 pm    Post subject: This post has 1 review(s) Reply with quote

I am not a developer of CE so it is not something I have any control over in regards to what Dark Byte does or doesn't do to it. I also don't use CE much outside of basic memory stuff and light debugging so I don't have much input to give on its various features or changes to the Lua side of things. When it was first added I released some stuff for it to help show how to use it but I don't actively use it.

Regarding my preference with things in general, related to Lua, I don't care for Lua 5.2 or newer. I still use 5.1 (mainly via LuaJIT) in my personal projects because I feel it offers much better features over the language changes that took way too long to finally be added to the language when 5.2 (and newer) was finally released. I have no need for the newer features being added to the language at this point that outweighs sticking with LuaJIT.

What ParkourPenguin said is likely the reason its still there in the highlighter though.

_________________
- Retired.
Back to top
View user's profile Send private message Visit poster's website
AylinCE
Grandmaster Cheater Supreme
Reputation: 37

Joined: 16 Feb 2017
Posts: 1516

PostPosted: Sat Apr 19, 2025 2:07 am    Post subject: Reply with quote

@atom0s, Thanks for the answer.

As far as I understand, you are using older versions of Lua and still recommending newer versions.

On the other hand, what interested me was the integration of Lua with CE.
I could not really adopt Lua outside of CE.

Also, considering Lua in your case, I cannot calculate the layers. Because the level you are at (LuaJIT) reminds me of the following saying;

"LuaJIT is like a superhero costume for Lua: Faster, more powerful and more impressive!"

(I will not bother you unnecessarily by asking why it does not work with versions after 5.1.)

Yes, I think the marked status of "bit32" is a forgotten or "if needed" detail to be revisited.
Frankly, there is an alternative and I think it does not need to be defined.

_________________
Hi Hitler Different Trainer forms for you!
https://forum.cheatengine.org/viewtopic.php?t=619279
Enthusiastic people: Always one step ahead
Do not underestimate me Master: You were a beginner in the past
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites