Aramyh Aramyh - 7 months ago 29
Ruby Question

Invalid hex escape

There is my code :

var = "aa"
var2 = "bb"
var3 = "\x#{var}\x#{var2}"


And I want the "var3" to be hexadecimal.

But the error message is "Invalid hex escape". How can I fix that ?

Answer

Your example does not work because \x has “higher priority” than string interpolation.

vars = [var, var2]
var3 = vars.pack('H*' * vars.size)
#⇒ "\xAA\xBB"

More info.