Quick Search for:  in language:    
SQL,PLSQL,Enable,easily,converts,frombinaries
   Code/Articles » |  Newest/Best » |  Community » |  Jobs » |  Other » |  Goto » | 
CategoriesSearch Newest CodeCoding ContestCode of the DayAsk A ProJobsUpload
.Net Stats

 Code: 157,764. lines
 Jobs: 478. postings

 How to support the site

 
Sponsored by:

 
You are in:
 
Login





Latest Code Ticker for .Net.
Click here to see a screenshot of this code!Weather Buddy
By Roger Martin on 1/28

(Screen Shot)

On The Fly Image Resizing, And Saveing
By Chris Hood on 1/27


Click here to see a screenshot of this code!Zip/View Project
By Mähr Stefan on 1/27

(Screen Shot)

Quick Audit
By Thomas Michael McGeown on 1/27


Click here to see a screenshot of this code!Kill Timer
By Adam( ) on 1/26

(Screen Shot)

Fibonacci Function
By Paddy Pasqualmie on 1/25


ID3v1.1 Reader
By Lewis Moten on 1/25


Click here to see a screenshot of this code!Lotto Picker
By Mick Doherty on 1/24

(Screen Shot)

Load a PopUnder using InternetExplore r
By Norberto Olazabal on 1/23


Click here to put this ticker on your site!


Add this ticker to your desktop!


Daily Code Email
To join the 'Code of the Day' Mailing List click here!

Affiliate Sites



 
 
   

Oracle PL/SQL Package - Numeric Convertion Functions

Print
Email
 

Submitted on: 11/28/2003 9:49:05 PM
By: Nulaya Knowledgements 
Level: Beginner
User Rating: Unrated
Compatibility:C#, VB.NET, ASP.NET, C++.NET

Users have accessed this code 1505 times.
 
 
     Enable you to easily converts to and from binaries, decimals, octals, hexadecimals. Using both SQL and PL/SQL
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
1) You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.   
2) You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
3) You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
4) You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.

    //**************************************
    //     
    // Name: Oracle PL/SQL Package - Numeric
    //     Convertion Functions
    // Description:Enable you to easily conv
    //     erts to and from
    binaries, decimals, octals, hexadecimals.
    Using both SQL and PL/SQL
    // By: Nulaya Knowledgements
    //
    // Inputs:The source numeric representat
    //     ion
    //
    // Returns:The target numeric representa
    //     tion
    //
    // Assumes:Your oracle account must be a
    //     ble to create a package object
    //
    // Side Effects:VALUE_ERROR if the input
    //     is out of range
    //
    //This code is copyrighted and has    // limited warranties.Please see http://
    //     www.Planet-Source-Code.com/vb/scripts/Sh
    //     owCode.asp?txtCodeId=1841&lngWId;=10    //for details.    //**************************************
    //     
    
    rem --
    rem -- Package Specification - Note that
    //     the pragma enable the
    rem -- function to be called from SQL.
    create or replace package nk_conv
    timestamp '1998-07-07:10:10:10'
    is
    ------------------------------------------------------------------
    -- 1998 Nulaya Knowledgements
    ------------------------------------------------------------------
    -- Version: 2.0.2
    ------------------------------------------------------------------
    -- Collections, Records, Variables, Constants, Exceptions, Cursors
    ------------------------------------------------------------------
    ------------------------------------------------------------------
    -- DEC2BIN - Converts decimal to binary
    ------------------------------------------------------------------
    function dec2bin(fv_value in integer) return integer;
    pragma restrict_references(dec2bin, WNDS, WNPS);
    ------------------------------------------------------------------
    -- BIN2DEC - Converts binary to decimal
    ------------------------------------------------------------------
    function bin2dec(fv_value in integer) return integer;
    pragma restrict_references(bin2dec, WNDS, WNPS);
    ------------------------------------------------------------------
    -- DEC2OCT - Converts decimal to octal
    ------------------------------------------------------------------
    function dec2oct(fv_value in integer) return integer;
    pragma restrict_references(dec2oct, WNDS, WNPS);
    ------------------------------------------------------------------
    -- OCT2DEC - Converts octal to decimal
    ------------------------------------------------------------------
    function oct2dec(fv_value in integer) return integer;
    pragma restrict_references(oct2dec, WNDS, WNPS);
    ------------------------------------------------------------------
    -- DEC2HEX - Converts decimal to hexadecimal
    ------------------------------------------------------------------
    function dec2hex(fv_value in integer) return varchar2;
    pragma restrict_references(dec2hex, WNDS, WNPS);
    ------------------------------------------------------------------
    -- HEX2DEC - Converts hexadecimal to decimal
    ------------------------------------------------------------------
    function hex2dec(fv_value in varchar2) return integer;
    pragma restrict_references(hex2dec, WNDS, WNPS);
    ------------------------------------------------------------------
    -- DEC2ASC - Converts decimal to ascii sequence
    ------------------------------------------------------------------
    function dec2asc(fv_value in integer) return varchar2;
    pragma restrict_references(dec2asc, WNDS, WNPS);
    ------------------------------------------------------------------
    -- ASC2DEC - Converts ascii sequence to decimal
    ------------------------------------------------------------------
    function asc2dec(fv_value in varchar2) return integer;
    pragma restrict_references(asc2dec, WNDS, WNPS);
    end nk_conv;
    /
    show errors
    rem --
    rem -- Package Body
    rem --
    create or replace package body nk_conv
    timestamp '1998-07-07:10:10:10'
    is
    ------------------------------------------------------------------
    -- 1998 Nulaya Knowledgements
    ------------------------------------------------------------------
    -- Version: 2.0.2
    ------------------------------------------------------------------
    -- Collections, Records, Variables, Constants, Exceptions, Cursors
    ------------------------------------------------------------------
    gv_bin_indx constant varchar2(2) := '01';
    gv_bin_base constant integer := length(gv_bin_indx);
    gv_bin_exp constant integer := 32;
    gv_oct_indx constant varchar2(8) := '01234567';
    gv_oct_base constant integer := length(gv_oct_indx);
    gv_oct_exp constant integer := 10;
    gv_hex_indx constant varchar2(16) := '0123456789ABCDEF';
    gv_hex_base constant integer := length(gv_hex_indx);
    gv_hex_exp constant integer := 8;
    gv_asc_indx constant varchar2(32) := '0123456789ABCDEFGHJKLNPQRSTUVXYZ';
    gv_asc_base constant integer := length(gv_asc_indx);
    gv_asc_exp constant integer := 32;
    ------------------------------------------------------------------
    ----------------------- Private Session --------------------------
    ------------------------------------------------------------------
    ------------------------------------------------------------------
    ------------------------ Public Session --------------------------
    ------------------------------------------------------------------
    -- DEC2BIN
    ------------------------------------------------------------------
    function dec2bin(fv_value in integer)
    return integer
    is
    retval varchar2(32);
    lv_value integer := abs(fv_value);
    begin
    if (lv_value > (gv_bin_base ** gv_bin_exp)) then
    raise value_error;
    end if;
    loop
    retval := substr(gv_bin_indx, trunc(mod(lv_value, gv_bin_base)) + 1, 1) || retval;
    lv_value := trunc(lv_value / gv_bin_base);
    if (lv_value < gv_bin_base) then
    retval := substr(gv_bin_indx, lv_value + 1, 1) || retval;
    exit;
    end if;
    end loop;
    return to_number(retval);
    exception
    when others then raise;
    end dec2bin;
    ------------------------------------------------------------------
    -- BIN2DEC
    ------------------------------------------------------------------
    function bin2dec(fv_value in integer)
    return integer
    is
    retval integer := 0;
    lv_pos integer;
    begin
    if (fv_value < 0) then
    raise value_error;
    end if;
    for i in 1 .. length(fv_value) loop
    lv_pos := instr(gv_bin_indx, substr(fv_value, (length(fv_value) - i + 1), 1));
    retval := retval + ((nvl(lv_pos,0) - 1) * (gv_bin_base ** (i - 1)));
    end loop;
    return retval;
    exception
    when others then raise;
    end bin2dec;
    ------------------------------------------------------------------
    -- DEC2OCT
    ------------------------------------------------------------------
    function dec2oct(fv_value in integer)
    return integer
    is
    retval varchar2(10);
    lv_value integer := abs(fv_value);
    begin
    if (lv_value > (gv_oct_base ** gv_oct_exp)) then
    raise value_error;
    end if;
    loop
    retval := substr(gv_oct_indx, trunc(mod(lv_value, gv_oct_base)) + 1, 1) || retval;
    lv_value := trunc(lv_value / gv_oct_base);
    if (lv_value < gv_oct_base) then
    retval := substr(gv_oct_indx, lv_value + 1, 1) || retval;
    exit;
    end if;
    end loop;
    return to_number(retval);
    exception
    when others then raise;
    end dec2oct;
    ------------------------------------------------------------------
    -- OCT2DEC
    ------------------------------------------------------------------
    function oct2dec(fv_value in integer)
    return integer
    is
    retval integer := 0;
    lv_pos integer;
    begin
    if (fv_value < 0) then
    raise value_error;
    end if;
    for i in 1 .. length(fv_value) loop
    lv_pos := instr(gv_oct_indx, substr(fv_value, (length(fv_value) - i + 1), 1));
    retval := retval + ((nvl(lv_pos,0) - 1) * (gv_oct_base ** (i - 1)));
    end loop;
    return retval;
    exception
    when others then raise;
    end oct2dec;
    ------------------------------------------------------------------
    -- DEC2HEX
    ------------------------------------------------------------------
    function dec2hex(fv_value in integer)
    return varchar2
    is
    retval varchar2(8);
    lv_value integer := abs(fv_value);
    begin
    if (lv_value > (gv_hex_base ** gv_hex_exp)) then
    raise value_error;
    end if;
    loop
    retval := substr(gv_hex_indx, trunc(mod(lv_value, gv_hex_base)) + 1, 1) || retval;
    lv_value := trunc(lv_value / gv_hex_base);
    if (lv_value < gv_hex_base) then
    retval := substr(gv_hex_indx, lv_value + 1, 1) || retval;
    exit;
    end if;
    end loop;
    return lpad(retval, gv_hex_exp, '0');
    exception
    when others then raise;
    end dec2hex;
    ------------------------------------------------------------------
    -- HEX2DEC
    ------------------------------------------------------------------
    function hex2dec(fv_value in varchar2)
    return integer
    is
    retval integer := 0;
    lv_pos integer;
    begin
    if (nvl(length(replace(translate(fv_value, gv_hex_indx, '0'),'0','')),0) <> 0) then
    raise value_error;
    end if;
    for i in 1 .. length(fv_value) loop
    lv_pos := instr(gv_hex_indx, substr(fv_value, (length(fv_value) - i + 1), 1));
    retval := retval + ((nvl(lv_pos,0) - 1) * (gv_hex_base ** (i - 1)));
    end loop;
    return retval;
    exception
    when others then raise;
    end hex2dec;
    ------------------------------------------------------------------
    -- DEC2ASC
    ------------------------------------------------------------------
    function dec2asc(fv_value in integer)
    return varchar2
    is
    retval varchar2(64);
    lv_value integer := abs(fv_value);
    begin
    if (lv_value > (gv_asc_base ** gv_asc_exp)) then
    raise value_error;
    end if;
    loop
    retval := substr(gv_asc_indx, trunc(mod(lv_value, gv_asc_base)) + 1, 1) || retval;
    lv_value := trunc(lv_value / gv_asc_base);
    if (lv_value < gv_asc_base) then
    retval := substr(gv_asc_indx, lv_value + 1, 1) || retval;
    exit;
    end if;
    end loop;
    return retval;
    exception
    when others then raise;
    end dec2asc;
    ------------------------------------------------------------------
    -- ASC2DEC
    ------------------------------------------------------------------
    function asc2dec(fv_value in varchar2)
    return integer
    is
    retval integer := 0;
    lv_pos integer;
    begin
    if (nvl(length(replace(translate(fv_value, gv_asc_indx, '0'),'0','')),0) <> 0) then
    raise value_error;
    end if;
    for i in 1 .. length(fv_value) loop
    lv_pos := instr(gv_asc_indx, substr(fv_value, (length(fv_value) - i + 1), 1));
    retval := retval + ((nvl(lv_pos,0) - 1) * (gv_asc_base ** (i - 1)));
    end loop;
    return retval;
    exception
    when others then raise;
    end asc2dec;
    end nk_conv;
    /
    show errors

 
Report Bad Submission
Use this form to notify us if this entry should be deleted (i.e contains no code, is a virus, etc.).
Reason:
 
Your Vote!

What do you think of this code(in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor See Voting Log
 
Other User Comments

 There are no comments on this submission.
 
Add Your Feedback!
Note:Not only will your feedback be posted, but an email will be sent to the code's author in your name.

NOTICE: The author of this code has been kind enough to share it with you.  If you have a criticism, please state it politely or it will be deleted.

For feedback not related to this particular code, please click here.
 
Name:
Comment:

 

Categories | Articles and Tutorials | Advanced Search | Recommended Reading | Upload | Newest Code | Code of the Month | Code of the Day | All Time Hall of Fame | Coding Contest | Search for a job | Post a Job | Ask a Pro Discussion Forum | Live Chat | Feedback | Customize | .Net Home | Site Home | Other Sites | About the Site | Feedback | Link to the Site | Awards | Advertising | Privacy

Copyright© 1997 by Exhedra Solutions, Inc. All Rights Reserved.  By using this site you agree to its Terms and Conditions.  Planet Source Code (tm) and the phrase "Dream It. Code It" (tm) are trademarks of Exhedra Solutions, Inc.