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 value to string?

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting
View previous topic :: View next topic  
Author Message
Dr.Disrespect
Grandmaster Cheater
Reputation: 3

Joined: 17 Feb 2016
Posts: 526

PostPosted: Thu Jun 08, 2017 10:30 am    Post subject: How to convert hex value to string? Reply with quote

In CE lua script, how to convert hex value to string?
I know the hex value and encoding, but don't know how to convert them, vice versa.

PS: it's a non-English string.

Thanks in advance.

_________________
**************

A simple example is better then ten links. Very Happy
Back to top
View user's profile Send private message
FreeER
Grandmaster Cheater Supreme
Reputation: 53

Joined: 09 Aug 2013
Posts: 1091

PostPosted: Thu Jun 08, 2017 2:28 pm    Post subject: Reply with quote

hex like 0xFF being 255?

Code:
hexValue = 0xaF -- same as hexValue = 175
hexstr1 = string.format("%x", hexValue) -- af
hexstr2 = string.format("%X", hexValue) -- AF
hexstr3 = string.format("%#x", hexValue) -- 0xAF

hexValue1 = tonumber(hexstr1, 16)
hexValue2 = tonumber(hexstr2, 16)
hexValue3 = tonumber(hexstr3, 16)
hexValue4 = tonumber(hexstr3:sub(3), 16)

hexValue5 = tonumber(hexstr3:gsub('^0x',''), 16)
hexValue6 = tonumber(hexstr1:gsub('^0x',''), 16)

print(hexstr1)
print(hexstr2)
print(hexstr3)
print()
print(hexValue1)
print(hexValue2)
print(hexValue3)
print(hexValue4)
print()
print(hexValue5)
print(hexValue6)

-- you can create your own functions to do it like so
string.toHex = function(num)
  return string.format("%#X", num)
end
string.fromHex = function(str)
  return tonumber(str:lower():gsub('^0x',''), 16)
end

hexstr = string.toHex(255)
hexval = string.fromHex(hexstr)-- hexstr:fromHex()
print(tostring(hexval) .. " is " .. hexstr)
Back to top
View user's profile Send private message
Dr.Disrespect
Grandmaster Cheater
Reputation: 3

Joined: 17 Feb 2016
Posts: 526

PostPosted: Thu Jun 08, 2017 3:09 pm    Post subject: Reply with quote

FreeER wrote:
hex like 0xFF being 255?

Code:
hexValue = 0xaF -- same as hexValue = 175
hexstr1 = string.format("%x", hexValue) -- af
hexstr2 = string.format("%X", hexValue) -- AF
hexstr3 = string.format("%#x", hexValue) -- 0xAF

hexValue1 = tonumber(hexstr1, 16)
hexValue2 = tonumber(hexstr2, 16)
hexValue3 = tonumber(hexstr3, 16)
hexValue4 = tonumber(hexstr3:sub(3), 16)

hexValue5 = tonumber(hexstr3:gsub('^0x',''), 16)
hexValue6 = tonumber(hexstr1:gsub('^0x',''), 16)

print(hexstr1)
print(hexstr2)
print(hexstr3)
print()
print(hexValue1)
print(hexValue2)
print(hexValue3)
print(hexValue4)
print()
print(hexValue5)
print(hexValue6)

-- you can create your own functions to do it like so
string.toHex = function(num)
  return string.format("%#X", num)
end
string.fromHex = function(str)
  return tonumber(str:lower():gsub('^0x',''), 16)
end

hexstr = string.toHex(255)
hexval = string.fromHex(hexstr)-- hexstr:fromHex()
print(tostring(hexval) .. " is " .. hexstr)


Thanks for the reply, but I want to convert hex value to characters, and I don't think LUA can easily do that as in C#. Sad

_________________
**************

A simple example is better then ten links. Very Happy
Back to top
View user's profile Send private message
FreeER
Grandmaster Cheater Supreme
Reputation: 53

Joined: 09 Aug 2013
Posts: 1091

PostPosted: Thu Jun 08, 2017 3:18 pm    Post subject: Reply with quote

Like ascii values eg. 0x61 to 'A'?

Code:
print(string.char(0x61))


Do you need to handle unicode? Or just ascii but 2 byte characters? Knowing exactly what you are trying to do would make looking info up easier (I know some of the basic of using lua but I'm no expert, however I tend to be decent at using google lol)

panraven wrote some code before to turn a widestring (unicode) into it's bytes so I'm sure you can do something similar to convert from bytes to a string see http://forum.cheatengine.org/viewtopic.php?p=5723462#5723462
Back to top
View user's profile Send private message
Dr.Disrespect
Grandmaster Cheater
Reputation: 3

Joined: 17 Feb 2016
Posts: 526

PostPosted: Thu Jun 08, 2017 3:24 pm    Post subject: Reply with quote

FreeER wrote:
Like ascii values eg. 0x61 to 'A'?

Code:
print(string.char(0x61))


Do you need to handle unicode? Or just ascii but 2 byte characters? Knowing exactly what you are trying to do would make looking info up easier (I know some of the basic of using lua but I'm no expert, however I tend to be decent at using google lol)

panraven wrote some code before to turn a widestring (unicode) into it's bytes so I'm sure you can do something similar to convert from bytes to a string see http://forum.cheatengine.org/viewtopic.php?p=5723462#5723462


I am trying to convert hex value into traditional Chinese.
For example, in C#, i will first convert the hex value into bytes, then convert the bytes into string. Something like this:
Code:

string output = Encoding.GetEncoding("BIG5").GetString(codeBytes);
//codeBytes is a byte array converted from hex value


Can you understand what I mean? My English is not so good, sorry.

Also, I will take a look at the link and provide feedback. Thanks.

_________________
**************

A simple example is better then ten links. Very Happy
Back to top
View user's profile Send private message
FreeER
Grandmaster Cheater Supreme
Reputation: 53

Joined: 09 Aug 2013
Posts: 1091

PostPosted: Thu Jun 08, 2017 5:50 pm    Post subject: Reply with quote

Makes sense, though I don't see anything obvious from google other than utf8 which I assume won't work with BIG5 Smile

Perhaps someone else will be able to provide more help (or at least a more confident answer) however
Back to top
View user's profile Send private message
Dr.Disrespect
Grandmaster Cheater
Reputation: 3

Joined: 17 Feb 2016
Posts: 526

PostPosted: Thu Jun 08, 2017 7:51 pm    Post subject: Reply with quote

FreeER wrote:
Makes sense, though I don't see anything obvious from google other than utf8 which I assume won't work with BIG5 Smile

Perhaps someone else will be able to provide more help (or at least a more confident answer) however


Still appreciate the help. Smile

_________________
**************

A simple example is better then ten links. Very Happy
Back to top
View user's profile Send private message
panraven
Grandmaster Cheater
Reputation: 54

Joined: 01 Oct 2008
Posts: 941

PostPosted: Thu Jun 08, 2017 10:37 pm    Post subject: Reply with quote

It seems the newest ce 6.7 just released support Codepage String display,
but the codepage seems depend on user's os setting,
ie. the codepage the os to be used when handle non-unicode application,
in os regional and language setting.
It seems no other ce function to set an arbitrary codepage to force ce to display the string on user's machine.

In CE 6.7+ memory record, set string type, click [Codepage] checkbox.
In Lua
Code:

local mr = GetAddressList()[0] -- get mr
mr.String.Codepage = true -- set using codepage, given mr.Type == vtString



cecodepage.jpg
 Description:
 Filesize:  60.32 KB
 Viewed:  18665 Time(s)

cecodepage.jpg



_________________
- Retarded.
Back to top
View user's profile Send private message
Dr.Disrespect
Grandmaster Cheater
Reputation: 3

Joined: 17 Feb 2016
Posts: 526

PostPosted: Mon Jun 12, 2017 7:44 am    Post subject: Reply with quote

panraven wrote:
It seems the newest ce 6.7 just released support Codepage String display,
but the codepage seems depend on user's os setting,
ie. the codepage the os to be used when handle non-unicode application,
in os regional and language setting.
It seems no other ce function to set an arbitrary codepage to force ce to display the string on user's machine.

In CE 6.7+ memory record, set string type, click [Codepage] checkbox.
In Lua
Code:

local mr = GetAddressList()[0] -- get mr
mr.String.Codepage = true -- set using codepage, given mr.Type == vtString

Sorry for the late reply. Thanks for the example.

I really hope CE can use codepage tho. Smile

_________________
**************

A simple example is better then ten links. Very Happy
Back to top
View user's profile Send private message
Csimbi
I post too much
Reputation: 92

Joined: 14 Jul 2007
Posts: 3102

PostPosted: Sun Oct 17, 2021 4:19 am    Post subject: Reply with quote

Hi guys,
I have this code:
Code:
  local textBytes = ''
  local byte = readShortInteger(myAddress)
  int i = 0
  while i < 14 do
    byte = readShortInteger(myAddress+i)
    textBytes = textBytes .. string.format('%02X', byte) .. ' '
    i = i + 1
  end

The output it produces is:
Code:
55 FF8B FFEC FF8B 45 08 56 FF8B 48 3C 03 FFC8 0F FFB7

while the input was:
Code:
55 8B EC 8B 45 08 56 8B 48 3C 03 C8 0F B7 41 14


How would I convert without getting those extra FFs?
(I would like to get to a real int to hex conversion of a single BYTE only, it seems it takes a WORD.)

Thank you!
Back to top
View user's profile Send private message
Dark Byte
Site Admin
Reputation: 457

Joined: 09 May 2003
Posts: 25262
Location: The netherlands

PostPosted: Sun Oct 17, 2021 5:13 am    Post subject: Reply with quote

looks like the Pascal compiler decided to see shortint the same as smallint (it shouldn't)

i'll fix it in next version. (you can & it with 0xff)

_________________
Do not ask me about online cheats. I don't know any and wont help finding them.

Like my help? Join me on Patreon so i can keep helping
Back to top
View user's profile Send private message MSN Messenger
Csimbi
I post too much
Reputation: 92

Joined: 14 Jul 2007
Posts: 3102

PostPosted: Sun Oct 17, 2021 6:12 am    Post subject: Reply with quote

Dark Byte wrote:
looks like the Pascal compiler decided to see shortint the same as smallint (it shouldn't)

i'll fix it in next version. (you can & it with 0xff)

Yep, this did the trick, thanks!
Code:
    byte = readShortInteger(injectAddressNum+i) & 0xff
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