GOLF: hex string to dotted quad

M

Martin DeMello

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
 
B

Ben Nagy

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"

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

Not much, but it's a start.

ben
 
M

Max Muermann

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 ;)

Cheers,
Max
 
C

Carlos

Martin said:
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"

--
 
C

Carl Drinkwater

hex_ip.gsub(/../) {|s| "#{s.hex}."}[0..-2]
The deletion of the trailing '.' is so ugly... but it's a leetle bit
shorter ;)

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?
 
P

Pit Capitain

Max said:
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 ;)

String#hex is a nice idea.

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

Regards,
Pit
 
C

Carlos

Max said:
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 ;)

The best so far :). Further refining:

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

--
 
M

Max Muermann

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
 
C

Carl Drinkwater

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 :)

"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?
 
M

Max Muermann

You've broken it :)

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top