From c635c994d1b1725f2b6994e6538a33f0bf8c92e8 Mon Sep 17 00:00:00 2001 From: Mike Mackintosh Date: Mon, 26 Jan 2015 16:38:10 -0500 Subject: use the #to_ipv6 method, faster --- README.rdoc | 7 ++++++- lib/ipaddress/ipv4.rb | 5 ++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/README.rdoc b/README.rdoc index 0419f6c..2057d15 100644 --- a/README.rdoc +++ b/README.rdoc @@ -318,12 +318,17 @@ transform it in data format using the IPv4#data method: ip.data #=> "\254\020\n\001" -Finally, you can transform an IPv4 address into a format which is +Also, you can transform an IPv4 address into a format which is suitable to use in IPv4-IPv6 mapped addresses: ip.to_ipv6 #=> "ac10:0a01" +Finally, much like IPv4#to_ipv6 you can use to IPv4#to_h method to return a non-semicolon delineated string (useful with pcap/byte level usage): + + ip.to_h + #=> "ac100a01" + === Classful networks IPAddress allows you to create and manipulate objects using the old diff --git a/lib/ipaddress/ipv4.rb b/lib/ipaddress/ipv4.rb index 0cb30ab..a32a4a0 100644 --- a/lib/ipaddress/ipv4.rb +++ b/lib/ipaddress/ipv4.rb @@ -239,9 +239,8 @@ module IPAddress; # ip.to_h # #=> 0a000000 # - def hex - hex = @octets.map{|o| o.to_s(16).rjust(2, '0') }.join() - "#{hex}" + def hex(space=true) + "%.4x%.4x" % [to_u32].pack("N").unpack("nn") end alias_method :to_h, :hex alias_method :to_hex, :hex -- cgit v1.2.1