十進制“數字”轉換成十六進制數字有命令行工具嗎?

本帖最後由 u2on 於 2010-10-22 07:23 編輯

od, xxd 好像是將編碼的轉換, 不是數字本身的轉換,那對數字有什麼方法呢?

原標題
命令行如何能方便的將ipv4格式的網址變換為ipv6格式?


echo "122.226.192.65"| xxx > ipv6 address

本帖最後由 u2on 於 2010-10-22 07:24 編輯

help needed, thanks

TOP

回復 2# u2on
A simple perl script can do.

==== perl script ====
#!/usr/bin/perl

my $in = <STDIN>;
my $out = "";

my @a = split /\./,$in;
for( my $i=0; $i<=$#a; $i++ ) {
  my $h = sprintf("%x", 0 + $a[$i]);
  if( $i == 0 ) {
    $out = $h;
  } else {
    $out = $out.".".$h;
  }
}
print $out;
==============
Save the above to a file, e.g. to_hex.pl
chmod +x to_hex.pl

Usage:
echo "122.226.192.65"| ./to_hex.pl > ipv6.txt

ipv6.txt will contain: 7a.e2.c0.41

Not sure whether this is what you want.

TOP

#!/bin/sh

printf "%02x%02x%02x%02x\n" `echo "122.226.192.65" | tr "." " "`

TOP

Both solutions are very good.

Thanks a lot for help!

TOP