View previous topic :: View next topic |
Author |
Message |
Dr.Disrespect Grandmaster Cheater Reputation: 3
Joined: 17 Feb 2016 Posts: 526
|
Posted: Thu Jun 08, 2017 10:30 am Post subject: How to convert hex value to string? |
|
|
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. |
|
Back to top |
|
|
FreeER Grandmaster Cheater Supreme Reputation: 53
Joined: 09 Aug 2013 Posts: 1091
|
Posted: Thu Jun 08, 2017 2:28 pm Post subject: |
|
|
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 |
|
|
Dr.Disrespect Grandmaster Cheater Reputation: 3
Joined: 17 Feb 2016 Posts: 526
|
Posted: Thu Jun 08, 2017 3:09 pm Post subject: |
|
|
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#.
_________________
**************
A simple example is better then ten links. |
|
Back to top |
|
|
FreeER Grandmaster Cheater Supreme Reputation: 53
Joined: 09 Aug 2013 Posts: 1091
|
Posted: Thu Jun 08, 2017 3:18 pm Post subject: |
|
|
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 |
|
|
Dr.Disrespect Grandmaster Cheater Reputation: 3
Joined: 17 Feb 2016 Posts: 526
|
Posted: Thu Jun 08, 2017 3:24 pm Post subject: |
|
|
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. |
|
Back to top |
|
|
FreeER Grandmaster Cheater Supreme Reputation: 53
Joined: 09 Aug 2013 Posts: 1091
|
Posted: Thu Jun 08, 2017 5:50 pm Post subject: |
|
|
Makes sense, though I don't see anything obvious from google other than utf8 which I assume won't work with BIG5
Perhaps someone else will be able to provide more help (or at least a more confident answer) however
|
|
Back to top |
|
|
Dr.Disrespect Grandmaster Cheater Reputation: 3
Joined: 17 Feb 2016 Posts: 526
|
Posted: Thu Jun 08, 2017 7:51 pm Post subject: |
|
|
FreeER wrote: | Makes sense, though I don't see anything obvious from google other than utf8 which I assume won't work with BIG5
Perhaps someone else will be able to provide more help (or at least a more confident answer) however |
Still appreciate the help.
_________________
**************
A simple example is better then ten links. |
|
Back to top |
|
|
panraven Grandmaster Cheater Reputation: 55
Joined: 01 Oct 2008 Posts: 942
|
Posted: Thu Jun 08, 2017 10:37 pm Post subject: |
|
|
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
|
Description: |
|
Filesize: |
60.32 KB |
Viewed: |
18779 Time(s) |
|
_________________
- Retarded. |
|
Back to top |
|
|
Dr.Disrespect Grandmaster Cheater Reputation: 3
Joined: 17 Feb 2016 Posts: 526
|
Posted: Mon Jun 12, 2017 7:44 am Post subject: |
|
|
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.
_________________
**************
A simple example is better then ten links. |
|
Back to top |
|
|
Csimbi I post too much Reputation: 94
Joined: 14 Jul 2007 Posts: 3110
|
Posted: Sun Oct 17, 2021 4:19 am Post subject: |
|
|
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 |
|
|
Dark Byte Site Admin Reputation: 458
Joined: 09 May 2003 Posts: 25288 Location: The netherlands
|
Posted: Sun Oct 17, 2021 5:13 am Post subject: |
|
|
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 |
|
|
Csimbi I post too much Reputation: 94
Joined: 14 Jul 2007 Posts: 3110
|
Posted: Sun Oct 17, 2021 6:12 am Post subject: |
|
|
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 |
|
|
|