Operating System - HP-UX
1748235 Members
3654 Online
108759 Solutions
New Discussion юеВ

Re: Hexadecimal conversion

 
SOLVED
Go to solution
Sanjay Tailor
Frequent Advisor

Hexadecimal conversion

Hello,

I am trying to write a shell script. Is there a command ( utility ) that I can put that will take a number ( base ten ) and convery it to a hexadecimal number and also the other way around?

Also, is there a way to read a certain directory and copy certain files into another directory?

Thank you for your help,
Sanjay
6 REPLIES 6
Peggy Fong
Respected Contributor

Re: Hexadecimal conversion

I got this from a friend hope it helps you:

I have a c program I wrote to convert number between octal, hex and decimal. Maybe you'll find it handy. Save it to convert.c than do "cc convert.c -o convert. Than you can run it by just typing convert. I've included an example. Hope this helps.

main()
{
/* Declare variables */
int number ;
char ibase ;

printf ("Enter base of input number (h)ex, (d)ec, (o)ctal - ") ;
scanf ("%c", &ibase) ;

switch ( ibase )
{
case 'h' :
printf ("Enter the HEX number - ");
scanf ("%x", &number ) ;
printf ("Decimal = %d\nOctal = %o\n", number, number );
break ;

case 'd' :
printf ("Enter the DECIMAL number - ");
scanf ("%d", &number ) ;
printf ("Hex = %x\nOctal = %o\n", number, number );
break ;

case 'o' :
printf ("Enter the OCTAL number - ");
scanf ("%o", &number ) ;
printf ("Decimal = %d\nHex = %x\n", number, number );
break ;

}

}

Run "convert"
$ convert
Enter base of input number (h)ex, (d)ec, (o)ctal - h
Enter the HEX number - 20
Decimal = 32
Octal = 40

As for your second question there are many way to copy files from one directory to another
You can use find | cpio -pdm But read up on find in the "man" pages for this... I'm sure others can give you examples as well.

Peggy
Bill Hassell
Honored Contributor

Re: Hexadecimal conversion

For scripting, use bc (it accepts commands as stdin) or xd.

Since you mentioned 'certain' files in 'certain' directories, a standard cp command, perhaps with -p option to retain the original timestamps and permissions. cd to the source directory and use regular expressions to define 'certain' files.


Bill Hassell, sysadmin
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: Hexadecimal conversion

Hi Senjay,

Peggy has given you a c method but there is a way to do it from the shell using the 'bc' binary calculator command. Try This:

#! /usr/bin/sh

# ------------------ Hex to Decimal Converter

while [ $# -ge 1 ]
do
INPUT=$1
shift
echo "obase=10;ibase=16;${INPUT};quit" | bc
done

#! /usr/bin/sh

# ------------------ Decimal to Hex Converter

while [ $# -ge 1 ]
do
INPUT=$1
shift
echo "obase=16;ibase=10;${INPUT};quit" | bc
done

Each of these takes any number of input arguments and converts them to the other radix one per line.

Regards, Clay
If it ain't broke, I can fix that.
James R. Ferguson
Acclaimed Contributor

Re: Hexadecimal conversion

Hi Sanjay:

Another way:

# printf "%#d\n" 0xf

# printf "%#x\n" 15

...JRF...
Manikanda
Occasional Visitor

Re: Hexadecimal conversion

Hi,

 

Is there any Tandem SQL or TAL procedures or Cobol snippets to convert a decimal Number to Hexadecimal String.. There are easy steps available in C however we would like to have it in Tandem SQL or TAL or COBOL... Thanks ins advance..

 

 

Regards,

Manikanda

Dennis Handly
Acclaimed Contributor

Re: Hexadecimal conversion

You can also use the shell builtin typeset to convert:

typeset -i16 x=256; echo "$x (256 in hex)"

typeset -i10 y=16#100; echo "$y (0x100 in decimal)"