Ruby send string variable as hex

How am I to express variable as hex to send like-

a='00'
write("\x#{a}") => 0x00

Trying to include received string variable into command string of raw data and passed to com port like -

cmd="\x45\x#{a}\x01"
Send(cmd)

In Ruby

Thanks

Answers


The String#to_i function takes a base argument, which default to ten - but you can pass in sixteen instead. That'll get you the number you want as a number, rather than a string. From there, you can use the Integer#chr function to get the value you want - a string containing the character with the binary value represented by the original string.


Need Your Help

NLOG, Log to SQLite on windows Phone 8

c# sqlite windows-phone-8

I have a Windows Phone application in wich I want to add NLog. The data from Nlog I want to put in a SQLite database.

How get Week day name in custom format using vb.net/C#

c# vb.net string datetime weekday

Using following code get the current Day Name of a computer