Reputation: 4879
I might be going about this the wrong way, and this might end up being a duplicate of something else.
I have a string that I need to convert into binary/hex.
This is what I am trying to do:
2.0.0p247 :050 > "test".unpack("H*").first
"74657374"
the 74657374
is the string of hex characters that I need, but I need them in actual hex, not a string.
How do I get this into \x74\x65\x73\x74
?
I've tried pack
ing the pairs of hex character, but they end up back in string form. My goal is to parse a bunch of strings into hex and then write them out to a file.
Upvotes: 0
Views: 137
Reputation: 54674
You need to do nothing at all. "\x74\x65\x73\x74"
is just another representation of "test"
. Try in IRB:
"\x74\x65\x73\x74" == "test"
#=> true
Also consider:
$ ruby -e "File.open('test.txt', 'w'){|f| f.write 'test'}"
$ hexdump test.txt
0000000 74 65 73 74
0000004
Upvotes: 3