This program has a serious flaw: only if 16 bytes are collected from the input,
a new line with hex codes is printed. If the input string has a length that is
not a multiple of 16, the remaining bytes will not be handled.
A solution would be to duplicate the line
out << sprintf("%08x: %-50s %s\n", offset, hex, str)
somewhere at the end of the loop, so that any remaining buffers are printed.
This forces me to duplicate code, though, which is not what I want.
The obvious solution for me would be to change the line
This program has a serious flaw: only if 16 bytes are collected from the input,
a new line with hex codes is printed. If the input string has a length that is
not a multiple of 16, the remaining bytes will not be handled.
A solution would be to duplicate the line
out << sprintf("%08x: %-50s %s\n", offset, hex, str)
I think it's ok to duplicate a single line. If you are worried about
the two lines
getting out of sync, put the line in a function:
def printHex(out, offset, hex, str)
out << sprintf("%08x: %-50s %s\n", offset, hex, str)
end
Consider the following code which generates a hexdump of a string:
...
What would be the idiomatic solution for this in Ruby ? Any other comments on
the code are welcome as well, ofcourse
Delightful ! It's hard to think outside the box if you mainly have been
programming C for 20 years...
Thank you very much, I will learn a *lot* from this newsgroup, I'm sure
···
Sander Land <sander.land@gmail.com> wrote:
On 7/17/06, Gerr <gerald@frobble.com> wrote:
Hello,
Consider the following code which generates a hexdump of a string:
...
What would be the idiomatic solution for this in Ruby ? Any other comments on
the code are welcome as well, ofcourse
If you want to do things with 16 bytes at a time, you might as well
process them in groups of 16: