# Convert binary string to hex digits.
def bin_to_hex(s)
s.each_byte.map { |b| b.to_s(16).rjust(2, '0') }.join
end
# Convers hex string to binary string.
def hex_to_bin(s)
s.scan(/../).map { |x| x.hex.chr }.join
end
# HBase special 'convert and print' routine to get hex digits, process them and print.
def print_hex_to_bin(s)
Kernel.print "\"" + Bytes.toStringBinary(s.scan(/../).map { |x| x.hex.chr }.join.to_java_bytes) + "\"\n"
end
1条答案
按热度按时间k0pti3hp1#
以下代码实际解决了我的所有任务,包括所需的输出:
主要基于http://anthonylewis.com/2011/02/09/to-hex-and-back-with-ruby/