Home: Fun With Perl: Perl Golf:
hex dump



freddo
User

Mar 23, 2002, 3:12 AM


Views: 50072
hex dump

Hello everybody,

I was making my new T-Shirt, and came out with this stupid one:
Can you read this?
And are you The Geek Girl i seek for?
Please, tell me now...

46 72 65 64 64 6F
and i wanted to have this text printed as an hexdump on it.

my first shot was:

Code
perl -ne'printf("%02X ",ord$_)for(split//);' filename


which outputs:
43 61 6E 20 79 6F 75 20 72 65 61 64 20 74 68 69 73 3F 0A 41 6E 64 20 61 72 65 20 79 6F 75 20 54 68 65 20 47 65 65 6B 20 47 69 72 6C 20 69 20 73 65 65 6B 20 66 6F 72 3F 0A 50 6C 65 61 73 65 2C 20 74 65 6C 6C 20 6D 65 20 6E 6F 77 2E 2E 2E 0A 0A 34 36 20 37 32 20 36 35 20 36 34 20 36 34 20 36 46


Can you come with a shortest way to do it (we dont care about 0D 0A or other \n)?

And what about doing the reverse?
;---


freddo
User

Mar 23, 2002, 4:40 AM


Views: 50070
Re: [freddo] hex dump

Damn, i just figured that:

Code
perl -ne'printf("%02X ",ord)for(split//);' filename

can do it too, and btw, the perldoc perlrun gives nice tricks about -F....
;---


mhx
Enthusiast / Moderator

Mar 23, 2002, 7:49 AM


Views: 50067
Re: [freddo] hex dump


In Reply To

Code
perl -ne'printf("%02X ",ord)for(split//);' filename



First of all, you can get rid of another three characters by stripping all those needless parentheses and the semicolon:


Code
perl -ne'printf"%02X ",ord,for+split//' filename


Using -F won't help much, it's score is equal:


Code
perl -naF'' -e'printf"%02X ",ord,for@F' filename


But using a simple regex will save some three characters again:


Code
perl -ne'printf"%02X ",ord,for/./sg' filename


-- mhx

At last with an effort he spoke, and wondered to hear his own words, as if some other will was using his small voice. "I will take the Ring," he said, "though I do not know the way."

-- Frodo