[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

[QUIZ] hexdump (#171

Matthew Moss

7/25/2008 5:22:00 PM

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

The three rules of Ruby Quiz 2:

1. Please do not post any solutions or spoiler discussion for this
quiz until 48 hours have passed from the time on this message.

2. Support Ruby Quiz 2 by submitting ideas as often as you can! (A
permanent, new website is in the works for Ruby Quiz 2. Until then,
please visit the temporary website at

<http://splatbang.com/rub....

3. Enjoy!

Suggestion: A [QUIZ] in the subject of emails about the problem
helps everyone on Ruby Talk follow the discussion. Please reply to
the original quiz message, if you can.

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

## hexdump (#171)

_Quiz idea provided by Robert Dober._

This week's quiz should be quick and easy for experienced Rubyists,
and a good lesson for beginners. Your task this week is to write a
utility that outputs a hex dump of the input.

There are a number of hex dump utilities in existence, that go by the
names `hd`, `od`, `hexdump`... I'm sure there are more. Pick one you'd
like to reproduce: If you're on any variety of Unix or BSD (including
Mac OS X), you can get man pages from the command-line to see how they
work. On Windows, if you don't have one installed, you can check out
this [man page for hexdump][1] and use that as a model.

You are not required to implement all the various command-line
switches, but I should be able to run your script on a file and, as a
minimum, see output resembling this (view with fixed-width font for
best results):

0000000 6573 2074 6c68 0a73 7973 746e 7861 6f20
0000010 0a6e 6f63 6f6c 7372 6863 6d65 2065 6564
0000020 6573 7472 0a0a 6573 2074 7865 6170 646e
0000030 6174 0a62 6573 2074 6174 7362 6f74 3d70
0000040 0a32 6573 2074 6873 6669 7774 6469 6874
0000050 323d 220a 6573 2074 6574 7478 6977 7464
0000060 3d68 3836 0a0a 2022 2051 6f63 6d6d 6e61
0000070 2064 6f74 7220 6665 726f 616d 2074 6170
0000080 6172 7267 7061 7368 6120 646e 6c20 7369
0000090 2e74 6e0a 6f6e 6572 616d 2070 2051 7167
00000a0 0a7d 0a0a
00000a4

Your submission should accept input either from a named file (part of
the command-line arguments) or from standard input if no filename is
provided.

Finally, when submitting, make sure to describe what existing hex dump
program you are emulating/reproducing (if any), and what arguments to
your script are needed, if any, to produce the basic output above.


[1]: http://unixhelp.ed.ac.uk/CGI/man-cgi...


--
Matthew Moss <matthew.moss@gmail.com>

12 Answers

James Gray

7/25/2008 8:02:00 PM

0

On Jul 25, 2008, at 12:22 PM, Matthew Moss wrote:

> There are a number of hex dump utilities in existence, that go by the
> names `hd`, `od`, `hexdump`... I'm sure there are more. Pick one you'd
> like to reproduce=85

xxd is my favorite.

James Edward Gray II


Mikael Høilund

7/25/2008 8:56:00 PM

0

Will you be accepting golfed solutions? Of course you will. :)

-- a,b=%Q=Z,O^NPO\r4_PV\\PI\x15^-\x0\v=,email=%\%%%c\%115%%# Mikael
Hoilund, CTO
okay=%#;hmm=(0...a.size).map{|i|((a[i]-email[i]+2)%128).# of Meta.io
ApS from
chr}.join;!email.gsub!'o',"%c%c"%[3+?0.<<(2),?G.~@];aha=#############
Denmark
hmm.scan(/#{'(.)'*5}/);!puts(email[1..-12]+aha.shift.zip(*aha).join)#
Ruby <3


Chris Shea

7/25/2008 9:37:00 PM

0

On Jul 25, 11:22 am, Matthew Moss <matthew.m...@gmail.com> wrote:
>     0000000 6573 2074 6c68 0a73 7973 746e 7861 6f20
>     0000010 0a6e 6f63 6f6c 7372 6863 6d65 2065 6564
>     0000020 6573 7472 0a0a 6573 2074 7865 6170 646e
>     0000030 6174 0a62 6573 2074 6174 7362 6f74 3d70
>     0000040 0a32 6573 2074 6873 6669 7774 6469 6874
>     0000050 323d 220a 6573 2074 6574 7478 6977 7464
>     0000060 3d68 3836 0a0a 2022 2051 6f63 6d6d 6e61
>     0000070 2064 6f74 7220 6665 726f 616d 2074 6170
>     0000080 6172 7267 7061 7368 6120 646e 6c20 7369
>     0000090 2e74 6e0a 6f6e 6572 616d 2070 2051 7167
>     00000a0 0a7d 0a0a
>     00000a4


Is this really what you dumped, Matthew? I was hoping for something a
little more... comprehensible.

Chris

---

es tlh
systnxao
nocolsrhcme eedestr

es txeapdnat
bes tatsbot=p
2es thsfiwtdiht2="
es tettxiwtd=h86

" Qocmmna dotr feroam taparrgpasha dnl si.tn
oneram p Qqg
}

Matthew Moss

7/26/2008 3:25:00 AM

0

On Jul 25, 4:34=A0pm, Chris Shea <cms...@gmail.com> wrote:
> On Jul 25, 11:22=A0am, Matthew Moss <matthew.m...@gmail.com> wrote:
>
> > =A0 =A0 0000000 6573 2074 6c68 0a73 7973 746e 7861 6f20
> > =A0 =A0 0000010 0a6e 6f63 6f6c 7372 6863 6d65 2065 6564
> > =A0 =A0 0000020 6573 7472 0a0a 6573 2074 7865 6170 646e
> > =A0 =A0 0000030 6174 0a62 6573 2074 6174 7362 6f74 3d70
> > =A0 =A0 0000040 0a32 6573 2074 6873 6669 7774 6469 6874
> > =A0 =A0 0000050 323d 220a 6573 2074 6574 7478 6977 7464
> > =A0 =A0 0000060 3d68 3836 0a0a 2022 2051 6f63 6d6d 6e61
> > =A0 =A0 0000070 2064 6f74 7220 6665 726f 616d 2074 6170
> > =A0 =A0 0000080 6172 7267 7061 7368 6120 646e 6c20 7369
> > =A0 =A0 0000090 2e74 6e0a 6f6e 6572 616d 2070 2051 7167
> > =A0 =A0 00000a0 0a7d 0a0a
> > =A0 =A0 00000a4
>
> Is this really what you dumped, Matthew? I was hoping for something a
> little more... comprehensible.
>
> Chris
>
> ---
>
> es tlh
> systnxao
> nocolsrhcme eedestr
>
> es txeapdnat
> bes tatsbot=3Dp
> 2es thsfiwtdiht2=3D"
> es tettxiwtd=3Dh86
>
> =A0" Qocmmna dotr feroam taparrgpasha dnl si.tn
> oneram p Qqg
>

I believe your endianness is off, sir.

Chris Shea

7/26/2008 6:14:00 AM

0

On Jul 25, 9:25 pm, Matthew Moss <matthew.m...@gmail.com> wrote:
> On Jul 25, 4:34 pm, Chris Shea <cms...@gmail.com> wrote:
>
>
>
> > On Jul 25, 11:22 am, Matthew Moss <matthew.m...@gmail.com> wrote:
>
> > >     0000000 6573 2074 6c68 0a73 7973 746e 7861 6f20
> > >     0000010 0a6e 6f63 6f6c 7372 6863 6d65 2065 6564
> > >     0000020 6573 7472 0a0a 6573 2074 7865 6170 646e
> > >     0000030 6174 0a62 6573 2074 6174 7362 6f74 3d70
> > >     0000040 0a32 6573 2074 6873 6669 7774 6469 6874
> > >     0000050 323d 220a 6573 2074 6574 7478 6977 7464
> > >     0000060 3d68 3836 0a0a 2022 2051 6f63 6d6d 6e61
> > >     0000070 2064 6f74 7220 6665 726f 616d 2074 6170
> > >     0000080 6172 7267 7061 7368 6120 646e 6c20 7369
> > >     0000090 2e74 6e0a 6f6e 6572 616d 2070 2051 7167
> > >     00000a0 0a7d 0a0a
> > >     00000a4
>
> > Is this really what you dumped, Matthew? I was hoping for something a
> > little more... comprehensible.
>
> > Chris
>
> > ---
>
> > es tlh
> > systnxao
> > nocolsrhcme eedestr
>
> > es txeapdnat
> > bes tatsbot=p
> > 2es thsfiwtdiht2="
> > es tettxiwtd=h86
>
> >  " Qocmmna dotr feroam taparrgpasha dnl si.tn
> > oneram p Qqg
>
> I believe your endianness is off, sir.

Whoops. I knew it looked almost like a slightly shuffled
somethingorother.

Chris

Matthew Moss

7/26/2008 1:14:00 PM

0



On Jul 25, 3:56=A0pm, Mikael H=F8ilund <mik...@hoilund.org> wrote:
> Will you be accepting golfed solutions? Of course you will. :)

Well, sure... Though in this case, I'd somewhat prefer to see nicely
written solutions that offered up more command-line options, such as
those provided by the various utilities. Things like grouping by 1, 2
or 4 bytes; ASCII display; binary/octal; etc.

But golfed solutions are okay, as usual...

Robert Dober

7/27/2008 8:55:00 PM

0

Well here goes my reference implementation, in good ol' RQ tradition.
Nothing fancy here just 16 bytes per line
with hexaddresses and ASCII output at the right, like the System V hd command.

http://pastie....

Robert


--
http://ruby-smalltalk.blo...

There's no one thing that's true. It's all true.
--
Ernest Hemingway

Mikael Høilund

7/27/2008 9:51:00 PM

0

Oh hi, I just thought I'd golf a solution. I'm sure other people can =20
do a much better job than I making a full hexdumping suite, so I just =20=

had some fun. Can't seem to get it lower than 78 characters, =20
unfortunately.

i=3D0;$<.read.scan(/.{0,16}/m){puts"%08x "%i+$&.unpack('H4'*8).join(' =20=

');i+=3D16}

Expanded and parenthesified, clarified:

i =3D 0
ARGF.read.scan(/.{0,16}/m) {
puts(("%08x " % i) + $&.unpack('H4'*8).join(' '))
i +=3D 16
}

ARGF (aliased as $<) is the file handle of all file names given in the =20=

arguments concatenated, STDIN if none =97 exactly what we need. The =20
regex to scan matches between 0 and 16 characters (including newline) =20=

greedily. Change it to 1,16 if you don't want the empty line at the end.

Instead of letting the block to scan take an argument, I used a trick =20=

I picked up from the last Ruby Quiz I participated in (Obfuscated =20
Email), and use $& inside the block, which is the last regex match. =20
Saves two characters \o/

The unpack returns an array of eight strings, each of four characters, =20=

with the hexadecimal representation of the ASCII value of two =20
consecutive characters. Fun, fun, fun.


Martin Boese

7/28/2008 2:27:00 PM

0


I added an ascii column to your solution... now it's about twice the size ;=
=2D)

i =3D 0
$<.read.scan(/.{0,16}/m) {
puts(("%08x " % i) + $&.unpack('H4'*8).join(' ') + ' ['+
$&.split(//).collect { |c| c.inspect[1] =3D=3D 92 ? '.' :c }.join + =
']' )
i +=3D 16
}

On Sunday 27 July 2008 22:51:04 Mikael H=F8ilund wrote:
> Oh hi, I just thought I'd golf a solution. I'm sure other people can
> do a much better job than I making a full hexdumping suite, so I just
> had some fun. Can't seem to get it lower than 78 characters,
> unfortunately.
>
> i=3D0;$<.read.scan(/.{0,16}/m){puts"%08x "%i+$&.unpack('H4'*8).join('
> ');i+=3D16}
>
> Expanded and parenthesified, clarified:
>
> i =3D 0
> ARGF.read.scan(/.{0,16}/m) {
> puts(("%08x " % i) + $&.unpack('H4'*8).join(' '))
> i +=3D 16
> }
>
> ARGF (aliased as $<) is the file handle of all file names given in the
> arguments concatenated, STDIN if none =97 exactly what we need. The
> regex to scan matches between 0 and 16 characters (including newline)
> greedily. Change it to 1,16 if you don't want the empty line at the end.
>
> Instead of letting the block to scan take an argument, I used a trick
> I picked up from the last Ruby Quiz I participated in (Obfuscated
> Email), and use $& inside the block, which is the last regex match.
> Saves two characters \o/
>
> The unpack returns an array of eight strings, each of four characters,
> with the hexadecimal representation of the ASCII value of two
> consecutive characters. Fun, fun, fun.



Adam Shelly

7/30/2008 9:37:00 PM

0

On 7/28/08, Martin Boese <boesemar@gmx.de> wrote:
> On Sunday 27 July 2008 22:51:04 Mikael H=F8ilund wrote:
> > Oh hi, I just thought I'd golf a solution. I'm sure other people can
> > do a much better job than I making a full hexdumping suite, so I just
> > had some fun. Can't seem to get it lower than 78 characters,
> > unfortunately.
> >
>
> I added an ascii column to your solution... now it's about twice the size=
;-)
>
> i =3D 0
> $<.read.scan(/.{0,16}/m) {
> puts(("%08x " % i) + $&.unpack('H4'*8).join(' ') + ' ['+
> $&.split(//).collect { |c| c.inspect[1] =3D=3D 92 ? '.' :c }.join +=
']' )
> i +=3D 16
> }
>

I can't resist golf: I got Martin's solution down to 95 bytes (If you
take out the ascii column it's down to 71).

i=3D0;$<.read.scan(/.{0,16}/m){puts"%08x0 "%i+$&.unpack('H4'*8)*' '+' |
'+$&.tr('^ -~','.');i+=3D1}

Tricks: *' ' is a shorter version of .join(' ') for arrays,
and $&.tr('^ -~','.') says translate any character not between ' ' and
'~' (32 to 126) to a '.' That saved a ton over the
split/collect/inspect method. (By the way, map and dump save a few
bytes over collect and inspect)

I also did a more full-featured version that supports some command line opt=
ions

-Adam
----------------------------------------------------
#hexdump utility for RubyQuiz#171
USAGE=3D<<USAGE

Usage:
#{$0.split(/[\/\\]/)[-1]} [-n length] [-s skip] [-g group] [-w
width] [-a] file

Dumps <length> bytes of <file> in hex format, starting at offset <skip>.
Prints <width> bytes per line in groups of size <group>.
Prints the ascii on the right unless <-a> specified

Default is all bytes of $stdin in 16/2 format.

USAGE
begin
width=3D16
group=3D2
skip=3D0
length=3DFloat::MAX
do_ascii =3D true
file =3D $stdin

while (opt=3DARGV.shift)
if opt[0]=3D=3D?-
case opt[1]
when ?n
length=3DARGV.shift.to_i
when ?s
skip=3DARGV.shift.to_i
when ?g
group =3D ARGV.shift.to_i
when ?w
width =3D ARGV.shift.to_i
when ?a
do_ascii =3D false
else
raise ArgumentError,"invalid Option #{opt}"
end
else
file =3D File.new(opt)
end
end

n=3D0
ascii=3D''
file.read(skip)
file.each_byte{|b|
if n%width =3D=3D 0
print "%s\n%08x "%[ascii,n+skip]
ascii=3D'| ' if do_ascii
end
print "%02x"%b
print ' ' if (n+=3D1)%group=3D=3D0
ascii << "%s"%b.chr.tr('^ -~','.') if do_ascii
break if n>length
}
puts ' '*(((2+width-ascii.size)*(2*group+1))/group.to_f).ceil+ascii
#this is probably the most complicated line
#it pads out the line to get the remaining ascii to align:
# (2+width-ascii.size) is the number of bytes missing (the 2 is for the '=
| ')
# *(2*group+1) is the width of a group of bytes with the space
# /group.to_f divides by the number of groups
# .ceil rounds up, otherwise we misalign on partial groups

rescue =3D>x
puts USAGE, "ERROR: #{x}"
end