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.
74657374是我需要的十六进制字符串,但我需要它们实际的十六进制,而不是字符串。
How do I get this into \x74\x65\x73\x74
?
如何将其插入\ 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.
我已经尝试打包成对的十六进制字符,但它们最终以字符串形式返回。我的目标是将一堆字符串解析为十六进制,然后将它们写入文件。
1 个解决方案
#1
3
You need to do nothing at all. "\x74\x65\x73\x74"
is just another representation of "test"
. Try in IRB:
你根本不需要做任何事情。 “\ x74 \ x65 \ x73 \ x74”只是“测试”的另一种表现形式。尝试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
#1
3
You need to do nothing at all. "\x74\x65\x73\x74"
is just another representation of "test"
. Try in IRB:
你根本不需要做任何事情。 “\ x74 \ x65 \ x73 \ x74”只是“测试”的另一种表现形式。尝试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