Convert 32 bit hex value into fields in decimal

I have 32 bit value in hex that I want to separate into fields and then convert the fields into decimal values.

Input file has 2 words of 32 bit hex values:

000001ac
ca85210e

Output both words separated into individual bit fields:

ca85210e: f1(31:9), f2(8:0)
f7c392ac:  f1(31:14), f2(13:6), f3(5:3), f4(2), f5(1), f6(0)

Output file should look like this with original hex value and fields in decimal:

ca85210e: 0, 428
f7c392ac: 253710, 74, 5, 1, 0, 0

Thank You,
morrbie

I think you have a cut and paste issue with your sample data. Going on what I believe you meant, this will read two lines from stdin and output the 'words' from each line using the indicated bit masking:

#!/usr/bin/env ksh


l=0
while read word
do
    if (( l < 1 ))
    then
        printf "%08x %d %d\n" 0x$word $(( 0x$word >> 9 )) $(( 0x$word & 0x1ffff ))
    else
        printf "%08x %d %d %d %d %d %d\n"  0x$word $(( 0x$word >> 14 )) $(( (0x$word >> 6)  & 0xff)) $(( (0x$word >> 3)  & 0x07)) $(( (0x$word >> 2)  & 0x01)) $(( (0x$word >> 1)  & 0x01)) $(( 0x$word & 0x01 ))
        break
    fi

    l=1
done

---------- Post updated at 21:07 ---------- Previous update was at 21:03 ----------

From your original post:

Shouldn't the two values in the file have been

000001ac
f7c392ac

given the output you listed, or the output should have been:

000001ac 0 428
ca85210e 207380 132 1 1 1 0

---------- Post updated at 21:16 ---------- Previous update was at 21:07 ----------

And one more note about the solution I posted. I originally read into word and assigned 0x to the variable so that I wouldn't need to prefix $word in each expression. This resulted in interesting behaviour in ksh -- bash seemed to get it right both ways. I need to test with a current build of ksh -- the most recent build I have access to here is 2011/02.

Code that has issues in Kshell:

#!/usr/bin/env ksh
l=0
while read word
do
    word="0x$word"
    if (( l < 1 ))
    then
        printf "%08x %d %d\n" $word $(( $word >> 9 )) $(( $word & 0x1ffff ))
    else
        printf "%08x %d %d %d %d %d %d\n"  $word $(( $word >> 14 )) $(( ($word >> 6)  & 0xff)) $(( ($word >> 3)  & 0x07)) $(( ($word >> 2)  & 0x01)) $(( ($word >> 1)  & 0x01)) $(( $word & 0x01 ))
        break
    fi

    l=1
done

It prints the first output line incorrectly, but the second is correct. Don't know if anybody wants to verify or offer an explanation, but it would be appreciated.

Yes, the output was posted incorrectly. Sorry about that. I must have been in a rush. Forgot to cut and paste the correct hex words in the output.
It should be:

000001ac 0 428
ca85210e 207380 132 1 1 1 0

Thank You for the reply. I will try it out.