Binary, octal, decimal, and hexadecimal reciprocal conversion functions in Oracle

Source: Internet
Author: User

Recently, the work needs to see the binary format of some fields, simply write some of the transformation of the function to facilitate the invocation.

Record the code for use.

Create or replace package Pkg_digit_conv as-converts a string to its binary storage format function Fun_str2bin in the database (Par_str in Varcha  
        
  R2) return varchar2;  
  --Converts binary to 10-function Fun_bin2dec (Par_bin in varchar2) return varchar2;  
  --Converts binary to 8-function fun_bin2oct (Par_bin in varchar2) return varchar2;  
  --Converts binary to 16-function Fun_bin2hex (Par_bin in varchar2) return varchar2;  
  --Convert 10 into binary function Fun_dec2bin (Par_dec in varchar2) return varchar2;  
  --Convert 8 into binary function Fun_oct2bin (par_oct in varchar2) return varchar2;  
        
--Convert 16 into binary function Fun_hex2bin (Par_hex in varchar2) return varchar2;  
      
      
End Create or replace package body Pkg_digit_conv as-converts a string to its binary storage format function Fun_str2bin in the database (Par_str in Varc  
  HAR2) return varchar2 as V_strlen Pls_integer;  
  V_detemp number;  
  V_bintemp varchar2 (20);  
  V_retval VARCHAR2 (1000);  
    Begin select Length (PAR_STR) into the v_strlen from dual; For I in 1..  
      V_strlen Loop Select To_number (ASCII (substr (par_str,i,1)) into the v_detemp from dual;  
      Select Fun_dec2bin (v_detemp) into the v_bintemp from dual; V_retval: =nvl (V_retval, ") | |  
    V_bintemp;  
    End Loop;  
  return v_retval;  
        
  End  
  --Converts the binary to a 10-system function Fun_bin2dec (Par_bin in varchar2) return varchar2 as V_retval varchar2 (100); Begin select SUM (d) into V_retval from (select substr (par_bin,rownum,1) *power (2,length (par_bin)-rownum) d fro  
    M dual Connect by rownum <= Length (par_bin));  
  return v_retval;  
        
  End  
  --Converts the binary to a 8 binary function fun_bin2oct (par_bin in varchar2) return varchar2 as V_octlen Pls_integer;  
  V_octtemp VARCHAR2 (3);  
  V_retemp varchar2 (1);  
  V_retval VARCHAR2 (1000);   
          
    Begin select Ceil (Length (Par_bin)/3) into the V_octlen from dual; For I with 1..v_octlen Loop if (3*i<=length (par_bin)) then select SUBSTR (par_bin,0-3*i,3) into v_octtemp from dual;  
      else Select substr (Par_bin,0-length (par_bin), Length (Par_bin) -3*i+3) into the v_octtemp from dual;  
      End If;  
      Select Fun_bin2dec (v_octtemp) into the v_retemp from dual; V_retval: = v_retemp| |  
    NVL (V_retval, "");  
    End Loop;  
  return v_retval;  
        
  End  
  --Converts the binary to a 16 binary function Fun_bin2hex (par_bin in varchar2) return varchar2 as V_hexlen Pls_integer;  
  V_hextemp VARCHAR2 (4);  
  V_retemp varchar2 (1);  
  V_retval VARCHAR2 (1000);   
          
    Begin select Ceil (Length (Par_bin)/4) into the V_hexlen from dual; For I in 1..v_hexlen Loop if (4*i<=length (par_bin)) then select SUBSTR (par_bin,0-4*i,4) into V_hextemp F   
      ROM dual;  
        else Select substr (Par_bin,0-length (par_bin), Length (Par_bin) -4*i+4) into the v_hextemp from dual; Select substr (' 000 ' | |  
      v_hextemp,-4,4) into the v_hextemp from dual;  
      End If;  
         Case v_hextemp when ' 0000 ' THEN v_retemp: = ' 0 '; wheN ' 0001 ' THEN v_retemp: = ' 1 ';  
         When ' 0010 ' THEN v_retemp: = ' 2 ';  
         When ' 0011 ' THEN v_retemp: = ' 3 ';  
         When ' 0100 ' THEN v_retemp: = ' 4 ';  
         When ' 0101 ' THEN v_retemp: = ' 5 ';  
         When ' 0110 ' THEN v_retemp: = ' 6 ';  
         When ' 0111 ' THEN v_retemp: = ' 7 ';  
         When ' 1000 ' THEN v_retemp: = ' 8 ';  
         When ' 1001 ' THEN v_retemp: = ' 9 ';  
         When ' 1010 ' THEN v_retemp: = ' A ';  
         When ' 1011 ' THEN v_retemp: = ' B ';  
         When ' 1100 ' THEN v_retemp: = ' C ';  
         When ' 1101 ' THEN v_retemp: = ' D ';  
         When ' 1110 ' THEN v_retemp: = ' E ';  
      else v_retemp: = ' F ';  
      End case; V_retval: = v_retemp| |  
    NVL (V_retval, "");  
    End Loop;  
  return v_retval;  
        
  End  
    --converts 10 into binary function Fun_dec2bin (Par_dec in varchar2) return VARCHAR2 as Yushu number;  
    Retemp varchar2 (1);  
  retval VARCHAR2 (1000);  
   Begin select To_number (PAR_DEC) into the Yushu from dual; While Yushu >0 loops select mod (yushu,2) into retemp from dual; retval: = Retemp | |  
      NVL (retval, "");  
    Select Trunc (YUSHU/2) into the Yushu from dual;  
    End Loop;  
  return retval;  
        
  End  
  --converts 8 into binary function Fun_oct2bin (par_oct in varchar2) return varchar2 as V_octlen Pls_integer;  
  V_octchar varchar2 (1);  
  V_dectemp number: = 0;  
  V_retval VARCHAR2 (1000);  
    Begin select Length (PAR_OCT) into the v_octlen from dual;  
    For i in 1..v_octlen loop v_dectemp: = V_dectemp + to_number (substr (par_oct,i,1)) *power (8,v_octlen-i);  
    End Loop;  
    Select Fun_dec2bin (To_char (v_dectemp)) into the v_retval from dual;  
  return v_retval;  
        
  End  
  --converts 16 into binary function Fun_hex2bin (Par_hex in varchar2) return varchar2 as V_hexlen Pls_integer;  
  V_dectemp number;  
    Begin select Length (Par_hex) into the v_hexlen from dual; Select To_number (Par_hex,lpad (' x ', V_hexlen, ' X ')) into the v_dectemp from dual;  
    Return Fun_dec2bin (v_dectemp);  
        
End End

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/database/Oracle/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.