[20190930]oracle raw型別轉化number指令碼.txt

lfree發表於2019-10-04

[20190930]oracle raw型別轉化number指令碼.txt

--//寫一個簡單oracle raw轉化number指令碼,簡單說明:
--//輸入必須是c1,02 或者 c102,不支援c1,2格式。
--//raw2num.sh 指令碼放在最後.

--//測試:
$ cat otest.txt | xargs  -n 1 -I {}  bash -c "./raw2num.sh {};echo {} " | paste - -
0       80
1       c1,02
2       c1,03
25      c1,1a
123     c2,02,18
4100    c2,2a
-4100   3d,3c,66
41000000        c4,2a
-41000000       3b,3c,66
132004078       c5,02,21,01,29,4f
2.01    c1,03,02
.3      c0,1f
.00000125       be,02,1a
115.200003      c2,02,10,15,01,04
-.00000125      41,64,4c,66
-.3     3f,47,66
-1      3e,64,66
-5      3e,60,66
-20032  3c,63,65,45,66
-234.432        3d,63,43,3a,51,66
999999999999999999999999999999999999999900000000000000000000000000000000000000000000000000000000000000000000000000000000000000  ff,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64
-999999999999999999999999999999999999990000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 00,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02
.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001     80,02
-.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001    7f,64,66
123456789012345678901234567890123456789000      d5,0d,23,39,4f,5b,0d,23,39,4f,5b,0d,23,39,4f,5b,0d,23,39,4f,5b
-123456789012345678901234567890123456780000     2a,59,43,2d,17,0b,59,43,2d,17,0b,59,43,2d,17,0b,59,43,2d,17,0b
.123456789012345678901234567890123456789        c0,0d,23,39,4f,5b,0d,23,39,4f,5b,0d,23,39,4f,5b,0d,23,39,4f,5b
-.12345678901234567890123456789012345678        3f,59,43,2d,17,0b,59,43,2d,17,0b,59,43,2d,17,0b,59,43,2d,17,0b


$ cat otest.txt
80
c1,02
c1,03
c1,1a
c2,02,18
c2,2a
3d,3c,66
c4,2a
3b,3c,66
c5,02,21,01,29,4f
c1,03,02
c0,1f
be,02,1a
c2,02,10,15,01,04
41,64,4c,66
3f,47,66
3e,64,66
3e,60,66
3c,63,65,45,66
3d,63,43,3a,51,66
ff,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64
00,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02
80,02
7f,64,66
d5,0d,23,39,4f,5b,0d,23,39,4f,5b,0d,23,39,4f,5b,0d,23,39,4f,5b
2a,59,43,2d,17,0b,59,43,2d,17,0b,59,43,2d,17,0b,59,43,2d,17,0b
c0,0d,23,39,4f,5b,0d,23,39,4f,5b,0d,23,39,4f,5b,0d,23,39,4f,5b
3f,59,43,2d,17,0b,59,43,2d,17,0b,59,43,2d,17,0b,59,43,2d,17,0b


$ cat raw2num.sh
#! /bin/bash
#! oracle raw convert number
odebug=${ODEBUG:-0}

# process input parameter ,delete 0x and "," and all spaces. save to variable v_raw. and its length to variable v_len.
v_raw="$*"
v_raw=${v_raw//0x/}
v_raw=${v_raw//[, ]/}
v_len=${#v_raw}

v_tmp=$(( $v_len % 2 ))
if [ $v_tmp -ne 0 -o $v_len -gt 42 ]; then
    echo "$v_raw is illegal! v_len=$v_len or length of v_len>42"
    exit 2
fi

if [ $odebug -eq 1 ] ; then
    echo v_ram="$v_raw"
fi    

if [ "$v_raw" == "80" ]; then
    result=0
    echo "$result"
    exit 0
elif [ "$v_raw" \> "80" ]; then
    v_res="0."
    v_exp=$(printf "%2d" $(( "0x"${v_raw:0:2} - 0xc0 )))

    if [ $odebug -eq 1 ] ; then
        echo v_ram="$v_raw" v_exp="$v_exp" v_len="$v_len"
    fi    

    for ((i=2;i<$v_len;i+=2))
    do
        v_tmp=$(printf "%02d" $(( "0x"${v_raw:i:2} -0x1 )))
        if [ $v_tmp -lt 0 -o $v_tmp -gt 99 ]; then
            echo "$v_raw is illegal! offset $i = 0x${v_raw:i:2}"
            exit 3
        fi
        if [ $v_tmp -eq 0 -a $i -eq $(( $v_len - 2 )) ];then
            echo "$v_raw is illegal! offset $i = 0x${v_raw:i:2}"
            exit 3
        fi
        v_res=${v_res}${v_tmp}
    done
    #result=$(echo "scale=132; 100^${v_exp} * ${v_res} " | bc -l | tr -d '\n\\\r' | sed -e "s/\.\([0-9]*[1-9]\)0\+$/.\1/" -e "s/\.0\+$//")
else
#negative
    v_res="-0."
    v_exp=$(printf "%2d" $(( 0xff - "0x"${v_raw:0:2} -0xc0 )))

    ## substr last 2 char, normal is 0x66 (102)
    v_len=$(( $v_len - 2 ))
    v_last=$(printf "%02d" $(( "0x"${v_raw:v_len:2} )))

    if [ $odebug -eq 1 ] ; then
        echo v_ram="$v_raw" v_exp="$v_exp" v_len-2="$v_len" v_last="$v_last"
    fi    
    
    if [ "$v_last" != "102" -a $v_len -lt 40  ]; then
        echo "$v_raw is illegal! offset $v_len = 0x${v_raw:v_len:2}"
        exit 4
    fi

    for ((i=2;i<$v_len;i+=2))
    do
        v_tmp=$(printf "%02d" $(( 0x65 - "0x"${v_raw:i:2} )))
        if [ $v_tmp -lt 0 -o $v_tmp -gt 99 ]; then
            echo "$v_raw is illegal! offset $i = 0x${v_raw:i:2}"
            exit 3
        fi
        if [ $v_tmp -eq 0 -a $i -eq $(( $v_len - 2 )) ];then
            echo "$v_raw is illegal! offset $i = 0x${v_raw:i:2}"
            exit 3
        fi
        v_res=${v_res}${v_tmp}
    done
    
    if [ $v_len -eq 42 -a "$v_last" != '102' ]; then
        v_tmp=$(printf "%02d" $(( 101 - $v_last )))
        if [ $v_tmp -le 0 -o $v_tmp -gt 99 ]; then
            echo "$v_raw is illegal! offset $v_len = 0x${v_raw:v_len:2}"
            exit 3
        fi
        v_res=${v_res}${v_last}
    fi        
    #result=$(echo "scale=132; 100^${v_exp} * ${v_res} " | bc -l | tr -d '\n\\\r' | sed -e "s/\.\([0-9]*[1-9]\)0\+$/.\1/" -e "s/\.0\+$//")
fi

if [ $odebug -eq 1 ] ; then
    echo v_ram="$v_raw" v_exp="$v_exp" v_len="$v_len" v_res="$v_res"
fi    

result=$(echo "scale=132; 100^${v_exp} * ${v_res} " | bc -l | tr -d '\n\\\r' | sed -e "s/\.\([0-9]*[1-9]\)0\+$/.\1/" -e "s/\.0\+$//")

echo "$result"

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/267265/viewspace-2658888/,如需轉載,請註明出處,否則將追究法律責任。

相關文章