# rowid与10进制和2进制之间的转换

0*64^5 + 0*64^4 + 0*64^3 +  0*64^2 +  0*64^1 +  25*64^0 ＝ 25。即object_id为25。依次转换成4个10进制数后，再将这四个值转换成2进制。比如25，转成二进制就变为11001。

00000000000000000000000000011001了。

.................

g_64  varchar2(64) := 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';

function x64_to_dec(x64 varchar2) return varchar2 as
type t_chrs is table of varchar2(10) index by pls_integer;
type t_nums is table of number(10);
v_chrs t_chrs;
v_fmt  t_nums := t_nums(32,10,22,16);--total:80 二进制划分区间
v_r64p t_nums := t_nums(6, 3, 6, 3); --total:18 64进制划分区间(rowid)
v_ren  t_nums := t_nums(0, 0, 0, 0);
v_bins varchar2(80) := '';
v_trnx varchar2(1000) := '';

function spart(str varchar2, n int) return varchar2 as
v_begin integer := 0;
begin
for i in 1 .. n - 1 loop
v_begin := v_begin + v_r64p(i);
end loop;
return substr(str, v_begin + 1, v_r64p(n));
end spart;
begin
--获取rowid的分段列表6/3/6/3
for j in v_r64p.first .. v_r64p.last loop
v_chrs(j) := spart(x64, j);
end loop;

--转换为10进制
for i in 1 .. v_chrs.count loop
for j in 1 .. length(v_chrs(i)) loop
v_ren(i) := v_ren(i) + power(64, length(v_chrs(i)) - j) * (instr(g_64, substr(v_chrs(i), j, 1)) - 1);
end loop;
end loop;

for k in 1 .. v_ren.count loop
v_bins := v_bins || lpad(dec_to_bin(v_ren(k)), v_fmt(k), '0');
end loop;

--将拼接的二进制串划分为每个8bit、共10个的二进制串
--此结果即为dump(rowid)的结果
for i in 0 .. length(v_bins)/8 - 1 loop
v_trnx := v_trnx || ',' || bin_to_dec(substr(v_bins, i*8 + 1, 8));
end loop;
v_trnx := '/OFBR[' || v_ren(1) || ',' || v_ren(2) || ',' || v_ren(3) || ',' || v_ren(4) || ']/DUMP[' || v_trnx || ']';
return v_trnx;
end x64_to_dec;

........

SQL> select rowid, ntx.x64_to_dec(rowid), dump(rowid),
2         --length(ntx.x64_to_dec(rowid)) len,
3         dbms_rowid.rowid_object(rowid) object_id,
4         dbms_rowid.rowid_relative_fno(rowid) fno,
5         dbms_rowid.rowid_block_number(rowid) blkno,
6         dbms_rowid.rowid_row_number(rowid) rowno
7    from t
8   where rownum <= 1 ;

ROWID              NTX.X64_TO_DEC(ROWID)                                                            DUMP(ROWID)                                                                       OBJECT_ID        FNO      BLKNO      ROWNO
------------------ -------------------------------------------------------------------------------- -------------------------------------------------------------------------------- ---------- ---------- ---------- ----------
AAAO0gAAYAAAA8NAAA /OFBR[60704,24,3853,0]/DUMP[,0,0,237,32,6,0,15,13,0,0]                           Typ=69 Len=10: 0,0,237,32,6,0,15,13,0,0                                               60704         24       3853          0

• 博文量
257
• 访问量
1070143