converting decimal to hex in a ksh script

Posted on 1999-01-12
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: in hex is 168432661
Is there an easy wasy to do this?
Thanks in advance!
Question by:jwcorbett
LVL 51

Accepted Solution

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

Expert Comment

Comment Utility
first one should read:
   echo | awk -F. '{print $1*256*256*256)+($2*256*256)+($3*256)+$4;}

Author Comment

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

LVL 84

Expert Comment

Comment Utility
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
perl -ne 'printf"%08x",unpack"N",pack"C*",split/\./'
LVL 51

Expert Comment

Comment Utility
echo | 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


Expert Comment

Comment Utility
You can do the whole job without using awk or perl:

typeset -i a b c d     # declare fields to be integer
echo | read a b c d

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


Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Replacement selected text 2 47
How to know only "File created" with EventLog 3 29
only14 challenge 19 63
wait notify demo infinite loop 3 80
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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 (, 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, fr…

743 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

17 Experts available now in Live!

Get 1:1 Help Now