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

# Converting number to HEX value, part 2

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?
```    str := upper(str);  /* a-f ==> A-F */
/* determine valid characters for given radix */
valid := 1;
len := length(str);
i := 1;

while (valid !=0) loop
valid := instr(validChars, substr(str, i, 1));
i := i + 1;
end loop;

if (valid=0) then
retval := false;
i := to_number('invalid number');  /* Forces ORA-6502. */
else
retval := true;
end if;

return retval;
end isValidNumStr;

/* This function converts a number into a string in given radix.
Only non-negative integer should be passed as the argument num, and
radix must be a positive integer in [1, 16].
Otherwise, 'ORA-6502: PL/SQL: numeric or value error' is raised.
*/
dividend number;
divisor number;
remainder number(2);
numStr varchar2(2000);
begin
/* NULL NUMBER -> NULL hex string */
if(num is null) then
return null;
elsif (num=0) then  /* special case */
return '0';
end if;

/* invalid number or radix; force ORA-6502: PL/SQL: numeric or value err
*/
if (num<0) or (num!=trunc(num)) or
numStr := to_char(to_number('invalid number'));  /* Forces ORA-6502. */
return numStr;
end if;

dividend := num;

/* the actual conversion loop */
while(dividend != 0) loop
numStr := digitToString(remainder) || numStr;
end loop;

return numStr;

function toBinaryString(num in number) return varchar2 as
begin
end toBinaryString;

function toHexString(num in number) return varchar2 as
begin
end toHexString;

function toOctalString(num in number) return varchar2 as
begin
end toOctalString;

/* The parseInt() function is equivalent to TO_NUMBER() when called
without a radix argument.  This is consistent with what Java does.
*/
function parseInt(s in varchar2) return number as
begin
end parseInt;

/* Converts a string in given radix to a number */
function parseInt(s in varchar2, radix in number) return number as
str varchar2(2000);
len number;
decimalNumber number;
begin
/* NULL hex string -> NULL NUMBER */
if(s is null) then
return null;
end if;

/* Because isValidNumStr() expects a IN OUT parameter, must use an
intermediate variable str.  str will be converted to uppercase
inside isValidNumStr().
*/
str := s;
if (isValidNumStr(str, radix) = false) then
return -1;  /* Never executes because isValidNumStr forced ORA-6502. */
end if;

len := length(str);
decimalNumber := 0;

/* the actual conversion loop */
for i in 1..len loop
decimalNumber := decimalNumber*radix + digitToDecimal(substr(str, i,
1));
end loop;

return decimalNumber;
end parseInt;
end Lang_Integer;
/

grant execute on Lang_Integer to public;
```

#### Start the conversation

Send me notifications when other members comment.

## SearchDataManagement

• ### Databricks Data Ingestion Network brings data to lakehouse

Databricks Ingest entered a public preview in a move by Databricks to enable a lakehouse that combines the best features of the ...

• ### Lessons learned from Credit Karma GraphQL architecture

Credit Karma's vice president of engineering explains why and how the personal finance service is using the GraphQL data query ...

• ### EnterpriseDB looks to grow market for PostgreSQL

Enterprises are increasingly using the open source PostgreSQL database. Read in this Q&A where the growth is coming from and how ...

• ### Tableau 2020.1 highlighted by Viz Animations, Dynamic Parameters

Dynamic Parameters and Viz Animations highlight Tableau's first 2020 platform update and comes nearly four months after ...

• ### Embedded BI software creates common ground for diverse analytics

Learn how embedding separate business intelligence capabilities into one application empowers users to drill down, access and ...

• ### Embedded BI and analytics apps speed workflows, insight access

Embedded BI is fast becoming a focal point for analytics uses as data analysts, developers and business users enjoy faster ...

## SearchSAP

• ### Understand SAP ECC vs. HANA vs. S/4HANA vs. R/3

SAP ECC, HANA, S/4HANA and R/3 are all valuable tools in the IT world, but it's easy to get them confused. Learn about their ...

• ### 5 tips for a successful S/4HANA Cloud implementation

Moving to S/4HANA public cloud can help you save on IT maintenance and simplify real-time analysis. Here's a look at what you can...

• ### Take care of data before SAP S/4HANA migration

In this Q&A, Rajesh Rengarethinam of ERP security vendor Appsian discusses why reviewing data security and business processes are...

## SearchSQLServer

• ### SQL Server database design best practices and tips for DBAs

Good database design is a must to meet processing needs in SQL Server systems. In a webinar, consultant Koen Verbeeck offered ...

• ### SQL Server in Azure database choices and what they offer users

SQL Server databases can be moved to the Azure cloud in several different ways. Here's what you'll get from each of the options ...

• ### Using a LEFT OUTER JOIN vs. RIGHT OUTER JOIN in SQL

In this book excerpt, you'll learn LEFT OUTER JOIN vs. RIGHT OUTER JOIN techniques and find various examples for creating SQL ...

## TheServerSide.com

• ### CSS performance optimization in 5 easy steps

Here are five CSS performance tips to help you avoid website performance problems.

• ### Why the 8 Java primitive data types are not objects

What's the difference between Java primitive types and objects? To start, don't classify primitive types as objects. Let's ...

• ### A brief history of Java: How it forever changed programming

The Java programming language is an important tool in an application developer's quiver. But Java has also undergone many changes...

## SearchDataCenter

• ### Plan for a successful ITIL implementation

Corporate buy-in and training are essential for organizations that decide to adopt the ITIL framework. Admins should also help ...

• ### Learn the main Linux OS components

Linux is one operating choice to run on your infrastructure. Get started with these terms to discover how the OS works and how it...

• ### New Dell EMC PowerEdge servers are built for the rugged edge

New Dell Streaming Media Platform includes two small form factor PowerEdge servers and a Modular Data Center. Dell aims the ...

## SearchContentManagement

With new Google Drive integration, Adobe embeds free features for commenting and annotating PDFs inside Google Docs. Acrobat ...

• ### 4 benefits of business process automation

Companies are automating business processes to improve workflows and use technology effectively. Some benefits of BPA include ...

• ### Test yourself on the differences between SharePoint and OneDrive

Businesses use Microsoft SharePoint and OneDrive capabilities for a number of reasons. Test your knowledge on the differences ...

## SearchHRSoftware

• ### 15 essential employee engagement survey questions

Crafting survey questions that uncover employee engagement issues is challenging. Here are some key questions and a guide to how ...

• ### Trump's H-1B work visa actions disappoint ex-Disney workers

President Donald Trump arrived in office promising to reform the H-1B visa program. But his actions have fallen short of what ...

• ### How real-time analytics can benefit HR

For analytics to be truly useful, it must be embedded into user workflows. Learn how HR tools are incorporating such approaches ...

Close