Script for converting Hexadecimal IP to Decimal IP
Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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".
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.