Aramyh Aramyh - 11 months ago 69
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 ?


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.