Operating System - HP-UX
1753890 Members
7482 Online
108809 Solutions
New Discussion юеВ

Convert Decimal to Binary & Hex, and vice versa

 
SOLVED
Go to solution
Kong Kian Chay
Regular Advisor

Convert Decimal to Binary & Hex, and vice versa

Anybody knows of any commands / utilities / freewares for HP-UX 10.20 or 11.0 which can -
a)Convert from Decimal to Binary, & vice versa.
b)Convert from Decimal to Hexadecimal, & vice versa.
4 REPLIES 4
Thomas Kollig
Trusted Contributor
Solution

Re: Convert Decimal to Binary & Hex, and vice versa

Hi!

dtcalc can convert them. Or you can write a short C program using scanf and printf.

Bye, Thomas
Andreas Voss
Honored Contributor

Re: Convert Decimal to Binary & Hex, and vice versa

Hi,

you could use the bc command within a script ie:
-------------------------------
#!/bin/sh
# decimal to binary
dec2bin()
{
{
echo "obase=2"
echo "$1"
echo "quit"
}|bc
}

dec=255 # example
bin=$(dec2bin $dec)
echo $bin
-----------------------------------------
#!/bin/sh
# binary to decimal
bin2dec()
{
{
echo "ibase=2"
echo "$1"
echo "quit"
}|bc
}

bin=010101 # example
dec=$(bin2dec $bin)
echo $dec
-----------------------------------
#!/bin/sh
# decimal to hex
dec2hex()
{
{
echo "obase=16"
echo "$1"
echo "quit"
}|bc
}

dec=255 # example
hex=$(dec2hex $dec)
echo $hex
--------------------------------------
#!/bin/sh
# hex to decimal
hex2dec()
{
{
echo "ibase=16"
echo "$1"
echo "quit"
}|bc
}

hex=0A # example
dec=$(hex2dec $hex)
echo $dec

Regards
Jdamian
Respected Contributor

Re: Convert Decimal to Binary & Hex, and vice versa

I cannot resist to reply:

Use the "printf" command... Yes, it exist.

$ printf "dec -> hex %d = %x\n" 32 32

$ printf "hex -> dec %x = %d\n" 0x20 0x20
Kong Kian Chay
Regular Advisor

Re: Convert Decimal to Binary & Hex, and vice versa

Thanks for all the prompt & detailed replies. I really learned a lot from this forum & am very glad that I'm a member.