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 hex bytes to string?

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

Joined: 02 May 2018
Posts: 28

PostPosted: Tue Aug 13, 2019 7:46 pm    Post subject: How to convert hex bytes to string? Reply with quote

How can I convert hex bytes (used in AOB scanning) into a string? For example:
Code:
hexbytes = '68 65 6C 6C 6F'
str = HexToString(hexbytes)
-- str should be "hello"
Back to top
View user's profile Send private message
Corroder
Grandmaster Cheater
Reputation: 27

Joined: 10 Apr 2015
Posts: 994

PostPosted: Tue Aug 13, 2019 8:04 pm    Post subject: Reply with quote

Code:
function HexToString(hb)
 hb = hb:gsub("%s+", "")
 hb = string.gsub(hb, "%s+", "")
 return (hb:gsub('..', function (cc)
 return string.char(tonumber(cc, 16)) end))
end

hexbytes = '68 65 6C 6C 6F'
str = HexToString(hexbytes)
print(str)

_________________
Stealing Code From Stolen Code...
And Admit It.. Hmmm....Typically LOL
Back to top
View user's profile Send private message
Csimbi
I post too much
Reputation: 72

Joined: 14 Jul 2007
Posts: 2268

PostPosted: Wed Aug 14, 2019 2:48 am    Post subject: Reply with quote

@Kamd
So, the place where you read those bytes in the first place.
Is there a zero at the end?
If so, just read it as a string.
Back to top
View user's profile Send private message
FreeER
Grandmaster Cheater Supreme
Reputation: 42

Joined: 09 Aug 2013
Posts: 1089

PostPosted: Wed Aug 14, 2019 5:21 am    Post subject: Reply with quote

Even if there's not readString takes the number of bytes to read.
_________________
https://github.com/FreeER/ has a few CE related repos
Back to top
View user's profile Send private message
Csimbi
I post too much
Reputation: 72

Joined: 14 Jul 2007
Posts: 2268

PostPosted: Wed Aug 14, 2019 5:47 am    Post subject: Reply with quote

FreeER wrote:
Even if there's not readString takes the number of bytes to read.

No, readString does not take the number of bytes to read.
readString takes the maximum number of characters to read.
Meaning, it will stop reading at the NULL character, or, at MaxLength, whichever comes first.
Back to top
View user's profile Send private message
FreeER
Grandmaster Cheater Supreme
Reputation: 42

Joined: 09 Aug 2013
Posts: 1089

PostPosted: Wed Aug 14, 2019 7:48 am    Post subject: Reply with quote

true, same difference here assuming you only care about the first 5 bytes.
You'd give 5 and it'd stop whether there's a 0 after or not

_________________
https://github.com/FreeER/ has a few CE related repos
Back to top
View user's profile Send private message
Csimbi
I post too much
Reputation: 72

Joined: 14 Jul 2007
Posts: 2268

PostPosted: Wed Aug 14, 2019 11:26 am    Post subject: Reply with quote

Well, yes.
If you know for a fact that there is a terminating NULL and you don't care how long string you get, you could put there 255 Wink
Either way, depending on the use case, the original poster might get away without having to use a hex-to-string converter.
Back to top
View user's profile Send private message
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