Home > Enterprise >  Shell decimal to hexadecimal
Shell decimal to hexadecimal

Time:04-02

How can I print 55 as hexadecimal with echo from decimal number?

#equivalent to:
printf "%x" 55

#like this:
dec=55
hex=$(([##16]dec))

echo $[0xAA]

I searched a lot but did not find any echo snippets for the calculation from decimal to hexadecimal.

CodePudding user response:

How can I print 55 as hexadecimal with echo from decimal number?

It is not possible, echo as the name implies, echos what you give to it, it does no conversion.

like this:

Yes, you can write a utility to convert a decimal number into hexadecimal number in shell. There is no such builtin conversion.

CodePudding user response:

Try this:

#!/bin/sh

OUTPUT=`echo $1 |dc -e "16o?p"`

echo $OUTPUT

and i have:

user@raspberrypi:~ $ ./convert.sh 999

3E7

CodePudding user response:

It is an extensive tool and if extensive calculations have to be made, then printf must be used so I thought that it might be possible at least by a calculation path.

  • Related