GOLF: hex string to dotted quad

irb(main):021:0> hex_ip = "c0a80167"
=> "c0a80167"
irb(main):022:0> ip = hex_ip.scan(/../).map {|i| i.to_i(16)}.join(".")
=> "192.168.1.103"

martin

irb(main):070:0> [hex_ip.hex].pack('N').unpack('C*').join('.')
=> "192.168.1.103"

Not much, but it's a start.

ben

···

On 18:28 Wed 06 Dec , Martin DeMello wrote:

irb(main):021:0> hex_ip = "c0a80167"
=> "c0a80167"
irb(main):022:0> ip = hex_ip.scan(/../).map {|i| i.to_i(16)}.join(".")
=> "192.168.1.103"

hex_ip.gsub(/../) {|s| "#{s.hex}."}[0..-2]

The deletion of the trailing '.' is so ugly... but it's a leetle bit shorter :wink:

Cheers,
Max

···

On 12/6/06, Martin DeMello <martindemello@gmail.com> wrote:

irb(main):021:0> hex_ip = "c0a80167"
=> "c0a80167"
irb(main):022:0> ip = hex_ip.scan(/../).map {|i| i.to_i(16)}.join(".")
=> "192.168.1.103"

Martin DeMello wrote:

irb(main):021:0> hex_ip = "c0a80167"
=> "c0a80167"
irb(main):022:0> ip = hex_ip.scan(/../).map {|i| i.to_i(16)}.join(".")
=> "192.168.1.103"

ip = [hex_ip].pack("H*").unpack("C*").join(".")
=> "192.168.1.103"

···

--

hex_ip.gsub(/../) {|s| "#{s.hex}."}[0..-2]

The deletion of the trailing '.' is so ugly... but it's a leetle bit shorter :wink:

Shorter still :

hex_ip.scan(/../).map{|i|i.hex}*"."

···

--
Regards,
Carl Drinkwater | 29degrees Ltd

29degrees.co.uk - Bespoke Web Application Development.
codegolf.com - Can you craft the smallest code?

Max Muermann schrieb:

···

On 12/6/06, Martin DeMello <martindemello@gmail.com> wrote:

irb(main):021:0> hex_ip = "c0a80167"
=> "c0a80167"
irb(main):022:0> ip = hex_ip.scan(/../).map {|i| i.to_i(16)}.join(".")
=> "192.168.1.103"

hex_ip.gsub(/../) {|s| "#{s.hex}."}[0..-2]

The deletion of the trailing '.' is so ugly... but it's a leetle bit shorter :wink:

String#hex is a nice idea.

   hex_ip.scan(/../).map{|s|s.hex}*"."

Regards,
Pit

Max Muermann wrote:

···

On 12/6/06, Martin DeMello <martindemello@gmail.com> wrote:

irb(main):021:0> hex_ip = "c0a80167"
=> "c0a80167"
irb(main):022:0> ip = hex_ip.scan(/../).map {|i| i.to_i(16)}.join(".")
=> "192.168.1.103"

hex_ip.gsub(/../) {|s| "#{s.hex}."}[0..-2]

The deletion of the trailing '.' is so ugly... but it's a leetle bit shorter :wink:

The best so far :). Further refining:

hex_ip.gsub(/../) {"#{$&.hex}."}.chop

--

Shorter still :

hex_ip.scan(/../).map{|i|i.hex}*"."

Or, based on Carlos' optimisation (and I use the term very loosely in
the context of golfing):

hex_ip.scan(/../).map{$&.hex}*"."

Saves another two characters... and looks horribly perlish.

Max

Or, based on Carlos' optimisation (and I use the term very loosely in
the context of golfing):

hex_ip.scan(/../).map{$&.hex}*"."

Saves another two characters... and looks horribly perlish.

You've broken it :slight_smile:

"c0a80167".scan(/../).map{$&.hex}*"."
=> "103.103.103.103"

$& holds the last match of the regex, not the current iteration of map.

<digress>
On that note, it's a bit of a shame that $_ doesn't work more like it does in perl. It would be very useful when golfing to have a variable that holds the parameters passed to a block without having to explicitly include an argument list to receive them.

(Although, I suppose that variable would have to be an array to receive multiple arguments, so the advantage would be lost because of the extra bytes needed to get values out of that array.)

Oh well. Ruby is still good for golfing, it's winning a good few of the challenges on codegolf.com.
</digress>

···

--
Regards,
Carl Drinkwater | 29degrees Ltd

29degrees.co.uk - Bespoke Web Application Development.
codegolf.com - Can you craft the smallest code?

So I have. I guess I need to start writing unit tests for my golfing
efforts. What a sad state of affairs...

Anyway, here's another one (with a little help):

require 'scanf'
hex_ip.scanf('%2x'*4)*"."
=> "192.168.1.103"

max

···

On 12/6/06, Carl Drinkwater <carl@carldr.com> wrote:

> Or, based on Carlos' optimisation (and I use the term very loosely in
> the context of golfing):
>
> hex_ip.scan(/../).map{$&.hex}*"."
>
> Saves another two characters... and looks horribly perlish.

You've broken it :slight_smile: