I want to decode this:
string.format('%X', '123')
I’ve searched through devforum and still couldn’t find how to decode something like this. How to decode?
I want to decode this:
string.format('%X', '123')
I’ve searched through devforum and still couldn’t find how to decode something like this. How to decode?
string.format(‘string.format(%q)’, ‘string.format(%q)’) → “string.format(‘string.format(%q)’)”
actually forgot that it should be a number sorry if this confuses
You can use the string.gsub
function. Here is a snippet:
can you explain?
also, this doesn’t fix anything, it made it a lot worse
If it’s just a hex number without any other characters in the string this should work
tonumber(hexString, 16)
Basically what EgizianoEG said, but he wrapped it in a function.
The encoded Hex string consists of pairs of characters that each represent a single character after being decoded.
To decode the string, we are replacing each pair of characters with the decoded character using the string.char
function. This function converts the returned value of tonumber
, which is an ASCII character code, to the corresponding character. The second argument of tonumber
, ‘radix’, is set to 16 (the input is treated as a hexadecimal).
Sorry, but what gone wrong with you?
This code should decode the input hex correctly without any issues.
I tried your code
function HexDecode(HexStr: string): string?
if not HexStr:match("^[%x]+$") then return nil end
return (HexStr:gsub("%x%x", function(Digits) return string.char(tonumber(Digits, 16)::number) end))
end
local s = string.format('%X', '123')
print(s, HexDecode(s), tonumber(s, 16))
and this is what it prints…
7B { 123
function HexDecode(HexStr: string): string? if not HexStr:match("^[%x]+$") then return nil end return (HexStr:gsub("%x%x", function(Digits) return string.char(tonumber(Digits, 16)::number) end)) end local s = "FF" print(s, HexDecode(s), tonumber(s, 16))
This function doesn’t support non-ASCII character Conversions…
The encoded string s
is malformed because it is returning ‘7B’, which is the encoded hex for ‘{’, @ketrab2004
round_63 wanted a way to convert a hexidecimal number made by the code he gave into a regular number. So your code didn’t work because it expected a different input.
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.