JL Computer Consultancy
Decimal to Hex conversion function.
|
Prior to Aug 1999
|
Somewhere in the $ORACLE_HOME/rdbms/admin set of scripts there is, I am sure, a little procedure of function to convert a decimal representation of a number into a Hexadecimal string. I haven't yet got around to looking for it, and over the years I've managed to do with by using a little PL/SQL routine.
I have just rewritten the routine as a packaged PL/SQl function, which can be called from SQL, although it is not intended to be terribly efficient (more a cute little example of recursion in PL/SQL) so should not be used for heavy-duty, high-speed processing.
rem
rem Script: jpl_utils.sql
rem Author: Jonathan Lewis
rem Dated:
rem Purpose: Set of odds and ends.
rem
rem Notes:rem This version holds only a tested decimal to hex conversion functionrem The function is declared as pure, and can be called from SQL.rem
rem The return is a string representing a HEX number up to 12 digits longrem
rem Sample of use:rem select jpl_utils.decimal_to_hex(99);rem execute dbms_output.put_line(jpl_utils.decimal_to_hex(127));rem
create or replace package jpl_utils as
function decimal_to_hex(i_decimal in integer) return varchar2; pragma restrict_references (decimal_to_hex, wnds, wnps, rnps);end;
.
/
rem
rem This isn't supposed to be an efficient high-volumerem function, just a cutesy little demo of recursive PL/SQLrem There is NO eror trappingrem
create or replace package body jpl_utils as
function decimal_to_hex (i_decimal in integer)
return varchar2 is
v_result varchar2(12);
v_hex_digit varchar2(1);
v_quotient integer;
v_remainder integer;
begin
if (i_decimal < 10) then v_result := to_char(i_decimal); elsif (i_decimal < 16) thenv_result := chr(65+(i_decimal-10)); -- or 55 + idec
else v_remainder := mod(i_decimal,16);v_quotient := round((i_decimal - v_remainder) /16);
v_result := jpl_utils.decimal_to_hex(v_quotient) || jpl_utils.decimal_to_hex(v_remainder); end if;return v_result;
end decimal_to_hex;end jpl_utils;
.
/
最后修改时间:2020-04-16 14:52:31
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




