Q
Problem solve Get help with specific problems with your technologies, process and projects.

# Converting number to HEX value, part 1

Is there a piece of PL/SQL or function or group of functions where I can take a number between 1 and 1234567 and convert it into a HEX value?
I do not know of any built-ins in Oracle. However, a long time ago I stumbled into a package in the public domain that you can use. I have provided the source code below. I have used this package and I am publishing it here for your benefit without infringing on any copyright.
```create or replace package Lang_Integer as
/* The package is named loosely after a similar Java class,
java.lang.Integer; in addition, all public package functions
(except toRadixString() which has no Java equivalent) are named
after equivalent Java methods in the java.lang.Integer class.
*/

/* Convert a number to string in given radix.
Radix must be in the range [2, 16].
*/
pragma restrict_references (toRadixString, WNDS, WNPS, RNDS, RNPS);

/* Convert a number to binary string. */
function toBinaryString(num in number) return varchar2;
pragma restrict_references (toBinaryString, WNDS, WNPS, RNDS, RNPS);

/* Convert a number to hexadecimal string. */
function toHexString(num in number) return varchar2;
pragma restrict_references (toHexString, WNDS, WNPS, RNDS, RNPS);

/* Convert a number to octal string. */
function toOctalString(num in number) return varchar2;
pragma restrict_references (toOctalString, WNDS, WNPS, RNDS, RNPS);

/* Convert a string, expressed in decimal, to number. */
function parseInt(s in varchar2) return number;
pragma restrict_references (parseInt, WNDS, WNPS, RNDS, RNPS);

/* Convert a string, expressed in given radix, to number.
Radix must be in the range [2, 16].
*/
function parseInt(s in varchar2, radix in number) return number;
pragma restrict_references (parseInt, WNDS, RNDS);
end Lang_Integer;
/

create or replace package body Lang_Integer as
/* Takes a number between 0 and 15, and converts it to a string (character)
The toRadixString() function calls this function.

The caller of this function is responsible for making sure no invalid
number is passed as the argument.  Valid numbers include non-negative
integer in the radix used by the calling function.  For example,
toOctalString() must pass nothing but 0, 1, 2, 3, 4, 5, 6, and 7 as the
argument 'num' of digitToString().
*/
function digitToString(num in number) return varchar2 as
digitStr varchar2(1);
begin
if (num<10) then
digitStr := to_char(num);
else
digitStr := chr(ascii('A') + num - 10);
end if;

return digitStr;
end digitToString;

/* Takes a character (varchar2(1)) and converts it to a number.
The parseInt() function calls this function.

The caller of this function is responsible for maksing sure no invalid
string is passed as the argument.  The caller can do this by first
calling the isValidNumStr() function.
*/
function digitToDecimal(digitStr in varchar2) return number as
num number;
begin
if (digitStr >= '0') and (digitStr <= '9') then
num := ascii(digitStr) - ascii('0');
elsif (digitStr >= 'A') and (digitStr <= 'F') then
num := ascii(digitStr) - ascii('A') + 10;
end if;

return num;
end digitToDecimal;

/* Checks if the given string represents a valid number in given radix.
Returns true if valid; ORA-6502 if invalid.
*/
function isValidNumStr(str in out varchar2,radix in number) return boolean
as
validChars varchar2(16) := '0123456789ABCDEF';
valid number;
len number;
i number;
retval boolean;
begin
end if;
```
This was last published in June 2004

## Content

Find more PRO+ content and other member only offers, here.

#### Have a question for an expert?

Get answers from a TechTarget expert on whatever's puzzling you.

You will be able to add details on the next page.

#### Start the conversation

Send me notifications when other members comment.

## SearchDataManagement

• ### Streaming tool from StreamSets eyes data in motion for GDPR

StreamSets software for inspecting big data brings governance to data in motion. Such capabilities may find more use as the ...

• ### Data expert: GDPR deadline is an opportunity, not a burden

There is stress as the EU's General Data Protection Regulation compliance deadline nears, but the GDPR privacy movement is a good...

In big data news, we find Google TPUs, or Tensor Processing Units, offered as a cloud service, while LinkedIn is open sourcing a ...

• ### Rethinking analytics processes spurs enterprise innovation

By taking a fresh look at the makeup of their analytics organizations, enterprises can innovate their business models and take ...

• ### Diversified data sets for analytics deliver top results

Analytics teams should focus on data diversity to ensure that their projects deliver the most meaningful insights -- but they ...

• ### How to boost the value of BI in today's analytics landscape

Traditional BI reporting still gives businesses valuable information. But its value can be increased by incorporating it into a ...

## SearchSAP

• ### SAP Ariba Live focuses on procurement for purpose

SAP Ariba Live 2018 focused on the idea that businesses can use procurement technology to do good in the world; for example, by ...

• ### SAP debuts consumption-based pricing model for SAP Cloud

SAP Cloud Platform is now available as a consumption-based model, an alternative to the subscription model. SAP also updated the ...

SAP paid \$2.4 billion to acquire lead-to-money vendor CallidusCloud, and analysts agree that the significant price may be worth ...

## SearchSQLServer

SQL Operations Studio simplifies routine administration of SQL Server and Azure SQL databases, making database development and ...

• ### Meltdown and Spectre fixes eyed for SQL Server performance issues

Microsoft has responded to the Spectre and Meltdown chip vulnerabilities with patches and other fixes. But IT teams need to sort ...

• ### Five SQL Server maintenance steps you should take -- ASAP

Putting off SQL Server administration tasks can lead to database problems. Enact these often-neglected maintenance items to help ...

## TheServerSide.com

• ### Why the Waterfall or Agile debate will be around forever

Which is the right methodology to use for your project: Waterfall or Agile? The industry may be at peak Agile, as the ...

• ### Chef's InSpec 2.0 brings compliance automation to the cloud

Enterprises have been quick to adopt automation tools for development and deployment but only recently have organizations started...

• ### Application security vulnerabilities are often known exploits

How hard is it to secure an enterprise application? It's not hard, especially given the fact that most application security ...

## SearchDataCenter

• ### IBM cloud services to secure mainframes out to the edge

Big Blue will introduce IBM cloud services that use blockchain, containers and its z14 mainframes to deliver improved security ...

• ### Four disadvantages of hyper-converged infrastructure systems

Problems with scalability and unexpected licensing costs can create problems for organizations that deploy hyper-converged ...

• ### IBM Power9 servers seek market inroads to AI, cloud

IBM follows up its first Power9 server with a raft of systems designed to appeal to a wider array of markets -- most notably, AI ...

## SearchContentManagement

• ### Scrivito unveils serverless CMS product

By building the CMS with ReactJS, Scrivito gained attraction with development community, according to an analyst.

• ### Content personalization tools sharpen focus on customers

Content personalization isn't new; Amazon weaponized it, and Jeff Bezos is the world's richest man. New tools are putting it ...

• ### Leading brands see the need for personalized content

Content personalization continues to expand within companies as maturing technologies make it a viable marketing option for ...

## SearchHRSoftware

• ### Blockchain in HR remains in beta, outlook uncertain

Blockchain in HR verification is getting interest from universities, vendors and startups. There are powerful forces driving its ...

• ### How people analytics can improve HR effectiveness

Getting insight into your workforce can reveal everything from training issues to the reasons for turnover or missed corporate ...

• ### At Ceridian, role of CIO requires constant learning, adjusting

You might say Warren Perlman, CIO at Ceridian, a global HCM software company, has been preparing for the role of CIO all his life...

Close