Aramyh Aramyh - 2 years ago 145
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 Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download