程序艺术 2013-01-29
1.rawtohex,hextoraw
rawtohex表示将raw类型数据转换为16进制字符串(nvarchar类型,Typ=1),hextoraw表示将16进制字符串转换为raw类型。
注意:hextoraw的参数如果是字符串,会当作16进制数字对待;如果是数字,也会认为是16进制而不是10进制;
SQL> select hextoraw('13'),hextoraw(13),hextoraw('D') from dual;
HEXTORAW('13') HEXTORAW(13) HEXTORAW('D')
-------------- ------------ -------------
13 13 0D
以上前两列返回结果是相同的(十六进制13即十进制19的字符串表示形式);
第三列返回'0D'是因为这样才是一个完整字节;
实际上hextoraw返回的值总是偶数位的,每两位表示一个字节;
dump一下可以证明(Typ=23表示raw类型):
SQL> select dump(hextoraw('13')),dump(hextoraw(13)),dump(hextoraw('D')) from dual;
DUMP(HEXTORAW('13')) DUMP(HEXTORAW(13)) DUMP(HEXTORAW('D'))
-------------------- ------------------ -------------------
Typ=23 Len=1: 19 Typ=23 Len=1: 19 Typ=23 Len=1: 13
注意以下例子:
SQL> select to_number('AB','xx') from dual;
TO_NUMBER('AB','XX')
--------------------
171
SQL> select hextoraw('AB') from dual;
HEXTORAW('AB')
--------------
AB
SQL> select hextoraw(to_number('AB','xx')) from dual;
HEXTORAW(TO_NUMBER('AB','XX'))
----------------------------------------
0171
虽然to_number('AB','xx')是将十六进制字符串'AB'转换为10进制数171,但是作为hextoraw参数时又被认为是16进制数了
2。utl_raw.bit_and,bitand
bitand的参数是十进制数字,将输入参数转化为二进制后求与,返回值是数值型;
SQL> select bitand(10,25) from dual;
BITAND(10,25)
-------------
8
SQL> select dump(bitand(10,25)) from dual;
DUMP(BITAND(10,25))
-------------------
Typ=2 Len=2: 193,9
10=1100b,25=11001b,按位与结果是01000b=8(Typ=2表示number类型)
utl_raw.bit_and的参数和返回值都是raw类型,
SQL> select utl_raw.bit_and(hextoraw('a'),hextoraw('19')) from dual;
UTL_RAW.BIT_AND(HEXTORAW('A'),
--------------------------------------------------------------------------------
08
SQL> select dump(utl_raw.bit_and(hextoraw('a'),hextoraw('19'))) from dual;
DUMP(UTL_RAW.BIT_AND(HEXTORAW(
--------------------------------------------------------------------------------
Typ=23 Len=1: 8