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
ID: 1295388
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

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

Author Comment

ID: 1295390
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?

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

LVL 84

Expert Comment

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

Expert Comment

ID: 1295392
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

ID: 1295393
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 Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
repeatSeparator  java  challenge 13 58
difference between String.subString() and String.subSequence() 6 229
centeredAverage challenge 8 178
Detect file exist or not 3 176
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: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
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.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used.

789 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