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 


how to convert big endian to little endian(vice versa)?

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting
View previous topic :: View next topic  
Author Message
kucingkembar
Advanced Cheater
Reputation: 0

Joined: 08 Oct 2020
Posts: 71

PostPosted: Mon Apr 01, 2024 7:33 pm    Post subject: how to convert big endian to little endian(vice versa)? Reply with quote

i read this topic at https://forum.cheatengine.org/viewtopic.php?t=604168
and read this
ParkourPenguin wrote:
I'm pretty sure you meant "big endian." With regards to primitive data types composed of multiple bytes, "little endian" means the least significant byte is stored first (used by x86 architectures) and "big endian" means the most significant byte is stored first (not uncommon to see on emulators).
Code:
32-bit integer:
0x1234ABCD

big endian:
12 34 AB CD

little endian:
CD AB 34 12


I read the Dark Byte reply using assembly code, but it confusing myself,
is there a simple way to "convert" this?
Back to top
View user's profile Send private message
ParkourPenguin
I post too much
Reputation: 152

Joined: 06 Jul 2014
Posts: 4702

PostPosted: Mon Apr 01, 2024 7:57 pm    Post subject: Reply with quote

What specifically do you mean by "convert"? If you're looking for a value type, big endian types are builtin to CE now. Enable them in Edit -> Settings -> Extra Custom Types

If you just mean "convert" in general, then reverse the order of the bytes. What was first is now last, what was second is now second-to-last, etc.

_________________
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
LeFiXER
Grandmaster Cheater Supreme
Reputation: 20

Joined: 02 Sep 2011
Posts: 1069
Location: 0x90

PostPosted: Mon Apr 01, 2024 8:02 pm    Post subject: Reply with quote

You reverse the byte order between the two endiannesses.

12 34 AB CD in big endian becomes CD AB 34 12 in little endian. If you wish to do this programmatically it really depends on the data representation.
Code:

function reverseBytes(num)
    -- Convert integer to hexadecimal string
    local hexValue = string.format('%X', num) 

    -- Ensure the length of the hex string is even by adding a leading zero if necessary
    if #hexValue % 2 ~= 0 then
        hexValue = '0' .. hexValue
    end
    -- Reverse the byte order
    local reversedHex = ''
    for i = #hexValue, 1, -2 do
        reversedHex = reversedHex .. string.sub(hexValue, i-1, i)
    end
    return reversedHex
end

local num = 0x1234ABCD
printf('Big Endian:\t%X', num)

local converted = reverseBytes(num)
print('Little Endian:\t' .. converted)


Execute the code in Lua Engine window within Cheat Engine.
Back to top
View user's profile Send private message
kucingkembar
Advanced Cheater
Reputation: 0

Joined: 08 Oct 2020
Posts: 71

PostPosted: Mon Apr 01, 2024 8:08 pm    Post subject: Reply with quote

thank you for the reply,
is this "Enable them in Edit -> Settings -> Extra Custom Types" can be edited/exported to Lua script like readSmallInteger()?
Back to top
View user's profile Send private message
kucingkembar
Advanced Cheater
Reputation: 0

Joined: 08 Oct 2020
Posts: 71

PostPosted: Tue Apr 02, 2024 6:06 am    Post subject: Reply with quote

LeFiXER wrote:
You reverse the byte order between the two endiannesses.

12 34 AB CD in big endian becomes CD AB 34 12 in little endian. If you wish to do this programmatically it really depends on the data representation.
Code:

function reverseBytes(num)
    -- Convert integer to hexadecimal string
    local hexValue = string.format('%X', num) 

    -- Ensure the length of the hex string is even by adding a leading zero if necessary
    if #hexValue % 2 ~= 0 then
        hexValue = '0' .. hexValue
    end
    -- Reverse the byte order
    local reversedHex = ''
    for i = #hexValue, 1, -2 do
        reversedHex = reversedHex .. string.sub(hexValue, i-1, i)
    end
    return reversedHex
end

local num = 0x1234ABCD
printf('Big Endian:\t%X', num)

local converted = reverseBytes(num)
print('Little Endian:\t' .. converted)


Execute the code in Lua Engine window within Cheat Engine.


thank you LeFiXER for the reply,
your code is what i looking for,
i want to reply ASAP but something about "double post" prevent me to do it
Back to top
View user's profile Send private message
AylinCE
Grandmaster Cheater Supreme
Reputation: 37

Joined: 16 Feb 2017
Posts: 1522

PostPosted: Tue Apr 02, 2024 9:26 am    Post subject: Reply with quote

Address based results in this method:

Code:
address = 0x64F344C0
print("byte: "..readShortInteger(address))
print("word: "..readSmallInteger(address))
print("integer: "..readInteger(address))
print("int64: "..readQword(address))
print("pointer: "..readPointer(address))
print("float: "..readFloat(address))
print("double: "..readDouble(address))
print("string: "..readString(address))

function func2hex(reverse,num,space)
str = ""
  if reverse==true then
    str = string.format("%08X",num)
  else
    str = string.format("%08X",num):reverse():gsub("(.)(.)","%2%1")
  end
   if space==1 then
     str = (str):gsub("..", "%1 "):sub(1,-2)
   end
   return str
end

local num = readInteger(0x64F344C0)

print("numInteger: "..num)
print(func2hex(false,num,0))
print(func2hex(true,num,0))
print(func2hex(false,num,1))
print(func2hex(true,num,1))



_________________
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