DBAsupport.com Forums - Powered by vBulletin
Results 1 to 2 of 2

Thread: Number Conversion

  1. #1
    Join Date
    Apr 2003
    Location
    Vijayawada-INDIA
    Posts
    14

    Number Conversion

    Hi,

    sir by using function < to_number('ff','xx') > in oracle8i
    iam able to convert hexadecimal to decimal
    Plz help me in convertion of decimal to Hexadicamal

    is there any similar functions in oracle8

    thanking u

  2. #2
    Join Date
    Nov 2000
    Location
    Israel
    Posts
    268
    Hi,
    Try this:

    create or replace function to_base( p_dec in number, p_base in number )
    return varchar2
    is
    l_str varchar2(255) default NULL;
    l_num number default p_dec;
    l_hex varchar2(16) default '0123456789ABCDEF';
    begin
    if ( trunc(p_dec) <> p_dec OR p_dec < 0 ) then
    raise PROGRAM_ERROR;
    end if;
    loop
    l_str := substr( l_hex, mod(l_num,p_base)+1, 1 ) || l_str;
    l_num := trunc( l_num/p_base );
    exit when ( l_num = 0 );
    end loop;
    return l_str;
    end to_base;
    /


    create or replace function to_dec
    ( p_str in varchar2,
    p_from_base in number default 16 ) return number
    is
    l_num number default 0;
    l_hex varchar2(16) default '0123456789ABCDEF';
    begin
    for i in 1 .. length(p_str) loop
    l_num := l_num * p_from_base + instr(l_hex,upper(substr(p_str,i,1)))-1;
    end loop;
    return l_num;
    end to_dec;
    /
    show errors

    create or replace function to_hex( p_dec in number ) return varchar2
    is
    begin
    return to_base( p_dec, 16 );
    end to_hex;
    /
    create or replace function to_bin( p_dec in number ) return varchar2
    is
    begin
    return to_base( p_dec, 2 );
    end to_bin;
    /
    create or replace function to_oct( p_dec in number ) return varchar2
    is
    begin
    return to_base( p_dec, 8 );
    end to_oct;
    /
    It is better to ask and appear ignorant, than to remain silent and remain ignorant.

    Oracle OCP DBA 9i,
    C++, Java developer

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Click Here to Expand Forum to Full Width