RAWTOHEX Function in PL/SQL

The objective is to use the function RAWTOHEX in PL/SQL code.

Purpose :
This inbuilt function is beneficial for the conversion of a raw value into a character value in its hexadecimal format.

Syntax :

RAWTOHEX(x) 

where x – raw value to be converted to hexadecimal value and hexadecimal equivalent in the form of a string value is returned.

Here may be a simple procedure to demonstrate the usage of the function –



Example-1: Storing the hexadecimal converted value in a variable ‘random’.

CREATE PROCEDURE TEST_RAW2HEX
IS
   random varchar2(20);
BEGIN
   SELECT RAWTOHEX('JAVA') INTO random FROM dual;
   dbms_output.put_line('random = ' || random);
EXCEPTION
WHEN OTHERS
THEN
   dbms_output.put_line(Exception occurred.)
END TEST_RAW2HEX;

When the procedure is created, and it is run using the SQL script –

BEGIN
    TEST_RAW2HEX;
END;

We get the output as –

random = 5859 

This function accepts argument of any data type other than LONG, LONG RAW, CLOB, BLOB, or BFILE.

Here we see another example of a procedure for the function demonstration.

Example-2: Storing the hexadecimal converted values in variables ‘random_1’ & ‘random_2’.

CREATE PROCEDURE TEST1_RAW2HEX
IS
random_1 varchar2(12);
random_2 varchar2(12);
BEGIN
   SELECT RAWTOHEX('JAVA') INTO random_1 FROM dual;
   SELECT RAWTOHEX('CPP')  INTO random_2 FROM dual;

   dbms_output.put_line('random_1 = ' || random_1);
   dbms_output.put_line('random_2 = ' || random_2);
END TEST1_RAW2HEX;

When the procedure is created, and it is run using the SQL script –

BEGIN
    TEST1_RAW2HEX;
END;

We get the output as –

random_1 = 4A415641
random_2 = 435050 

Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.