Solved

converting decimal to hex in a ksh script

Posted on 1999-01-12
6
745 Views
Last Modified: 2013-12-26
In a KSH script, I need to take an IP address and convert it to host long, which is a HEX format.
for example: 10.10.20.21 in hex is 168432661
Is there an easy wasy to do this?
Thanks in advance!
0
Comment
Question by:jwcorbett
6 Comments
 
LVL 51

Accepted Solution

by:
ahoffmann earned 50 total points
ID: 1295388
for the result in your question (which is decimal not hex):
    echo 10.10.20.21 | awk -F. '{printf "0x%x", ($1*256*256*256)+($2*256*256)+($3*256)+$4;}
or if you want hex:
    echo 10.10.20.21 | awk -F. '{printf "0x%x", ($1*256*256*256)+($2*256*256)+($3*256)+$4;}
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 1295389
first one should read:
   echo 10.10.20.21 | awk -F. '{print $1*256*256*256)+($2*256*256)+($3*256)+$4;}
sorry.
0
 

Author Comment

by:jwcorbett
ID: 1295390
Thank you!
It works, with a little change in syntax, but for an IP of 10.10.20.21, I get an answer 168433e+08
How can I format the naswer to give the whole number?

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 84

Expert Comment

by:ozo
ID: 1295391
awk -F. '{printf "%d",($1*256*256*256)+($2*256*256)+($3*256)+$4;}'
 awk -F. '{printf "%08x",($1*256*256*256)+($2*256*256)+($3*256)+$4;}'
But this may fail for 128.10.20.21
perl -ne 'printf"%08x",unpack"N",pack"C*",split/\./'
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 1295392
echo 128.10.20.21 | awk -F. '{printf "%12.12d",($1*256*256*256)+($2*256*256)+($3*256)+$4;}'

works for me, but depending on your OS leading chars are blanks or zeros

0
 
LVL 3

Expert Comment

by:mliberi
ID: 1295393
You can do the whole job without using awk or perl:

#!/bin/ksh
IFSsave=$IFS
typeset -i a b c d     # declare fields to be integer
IFS='.'
echo 10.10.20.21 | read a b c d
IFS=$IFSsave

echo $(((a<<24)+(b<<16)+(c<<8)+d))
# if a>127 the result will be a negative number
exit



0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Expand macro to include alternate header placement and naming 5 66
repeateFront java challenge 31 92
iSeries FTP Exit Program 8 124
substring method in java 1 97
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now