我想从STDIN一次连续读取四个字节,并以十六进制输出。 我尝试过使用read,readbyte,each_byte,但我似乎无法让它工作。
cat file | ./processor.rb
0x...
0x...
...
其中file是二进制文件。
答案 0 :(得分:2)
STDIN.each_byte.each_slice(4) { |b4|
# Do something with `b4`. `b4` is an array that contains up to 4 bytes
}
或
STDIN.each_char.each_slice(4) { |c4|
# Do something with `c4`. `c4` is an array that contains up to 4 characters
}