LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-04-2008, 01:57 PM   #1
PKrishna
LQ Newbie
 
Registered: Mar 2008
Posts: 10

Rep: Reputation: 0
Smile Script for converting Hexadecimal IP to Decimal IP


Dear All,

Could someone help me with some script to convert Hexadecimal IPs to Decimal dotted IPs please?
I have a file with 2600 Hex IPs of the following format

15 41 01 C2
15 41 01 C6
15 41 01 C7


Please help me convert them into the following format


21.65.1.194
21.65.1.198
21.65.1.199

Thanks in advance.

Warm regards,
Krishna
 
Old 07-04-2008, 02:57 PM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
You can use bc specifying that the number is base-16, for example by something like
Code:
for i in 15 41 01 C2
do
  printf "%d." $(echo "ibase=16; $i" | bc)
done | sed s/.$//
The sed command serves to strip out the ending dot, but there should be a better way to do.
 
Old 07-04-2008, 03:25 PM   #3
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670
Code:
cat sample
15 41 01 C2
15 41 01 C6
15 41 01 C7

while read ip[0] ip[1] ip[2] ip[3]; do 
   for ((n=0; n<4; n++)); do 
      ip[(($n))]=$((0x${ip[$n]}))
   done
   echo "${ip[0]}.${ip[1]}.${ip[2]}.${ip[3]}"
done <sample

21.65.1.194
21.65.1.198
21.65.1.199
You might want to use something like
ipaddress="${ip[0]}.${ip[1]}.${ip[2]}.${ip[3]}" instead of an echo in a script. You could even write a similar routine as a function to take a string like "15 41 01 C2" and return a string like "21.65.1.194".
 
Old 07-04-2008, 05:11 PM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Following the suggestion by jschiwal, you can create a simple function like this:
Code:
hex2dec(){
  echo -n $(echo "ibase=16; $1" | bc).
  echo -n $(echo "ibase=16; $2" | bc).
  echo -n $(echo "ibase=16; $3" | bc).
  echo -n $(echo "ibase=16; $4" | bc)
}
or
Code:
hex2dec(){
  echo -n $((0x$1)).
  echo -n $((0x$2)).
  echo -n $((0x$3)).
  echo -n $((0x$4))
}
You can also include a check on the number and the type of the arguments. Thanks to jschiwal for pointing out the bash syntax to do the conversion. I didn't know it.
 
Old 07-04-2008, 05:24 PM   #5
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 61
Code:
$ cat ips

15 41 01 C2
15 41 01 C6
15 41 01 C7

$ perl -ne 'printf "%d.%d.%d.%d\n", map { hex($_)} split unless /^\s*$/' ips
21.65.1.194
21.65.1.198
21.65.1.199
 
Old 07-04-2008, 08:49 PM   #6
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670
Thanks for your contribution colucix. I made a small change and removed the "-n" option from the last echo statement.
Code:
cat bin/hex2decip
h2dip(){
  echo -n $((0x$1)).
  echo -n $((0x$2)).
  echo -n $((0x$3)).
  echo  $((0x$4))
}
jschiwal@hpamd64:~> source hex2decip
jschiwal@hpamd64:~> type hex2decip
hex2decip is /home/jschiwal/bin/hex2decip
jschiwal@hpamd64:~> type h2dip
h2dip is a function
h2dip ()
{
    echo -n $((0x$1)).;
    echo -n $((0x$2)).;
    echo -n $((0x$3)).;
    echo $((0x$4))
}
jschiwal@hpamd64:~> h2dip 12 1A 1b 22
18.26.27.34
I also included "ip" at the end of the name because it seems hex2dec is either taken or describes converting hexidecimal strings to their decimal equivalent. ( I think that DVD's use binary coded hex values, similar to bcd, for portability reasons. ) I also noticed how bash added the semicolons at the end of the statements. I hadn't noticed that before.

Last edited by jschiwal; 07-04-2008 at 08:53 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
C++, issues converting decimal to fraction alpha_gamma Programming 7 06-04-2007 08:15 PM
Decimal numbers in bash script variables? Massif Programming 3 11-07-2005 10:01 PM
Have problem converting a decimal number to octal Linh Programming 4 05-20-2004 04:21 PM
converting integer value to hexadecimal string in C - any suggestions?? woodywellhung Programming 3 04-24-2004 06:27 PM
converting fake hex to decimal in c acid_kewpie Programming 10 08-20-2003 03:29 PM


All times are GMT -5. The time now is 08:21 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration