[20190930]oracle raw类型转化number脚本.txt

[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"

posted @ 2019-10-04 20:31  lfree  阅读(677)  评论(0编辑  收藏  举报