The RAWTOHEX function in Oracle SQL is a data type conversion function. It is the direct opposite of HEXTORAW. Its one and only job is to take a RAW binary value and convert it into a human-readable hexadecimal text string.
This is an essential function for debugging, logging, or displaying binary data, as it allows you to "see" the raw bytes in a standard text format.
What is the RAWTOHEX Function in Oracle?
The RAWTOHEX(raw_value) function takes a RAW data type (which is a sequence of binary bytes) and returns a VARCHAR2 string. Each byte in the RAW value is converted into two hexadecimal characters.
For example, a single raw byte 7D (which is 01111101 in binary) is converted to the text string '7D'.
This function can also convert other data types (like VARCHAR2 or NUMBER) into their hex representations by first recasting them to RAW.
RAWTOHEX Function Syntax
The syntax for RAWTOHEX is very simple:
RAWTOHEX(raw_value)
Let's break that down:
raw_value: TheRAWvalue or column you want to convert. This can also be any other scalar data type (likeVARCHAR2,NUMBER,DATE), which Oracle will first convert toRAWand then to hex.
Oracle RAWTOHEX Function Examples
Here are two practical examples of how to use RAWTOHEX.
Example 1: Converting a RAW Value to Hex using RAWTOHEX
This example shows the most basic use. We'll use HEXTORAW to create a RAW value and then immediately use RAWTOHEX to convert it back to a text string.
Query:
SELECT
RAWTOHEX(HEXTORAW('7D4A9B')) AS "Hex_String"
FROM DUAL;
Result:
Hex_String
----------
7D4A9B
Example 2: Finding the Hex Representation of a Text String using RAWTOHEX
This is a very common use case. You can use RAWTOHEX to find the underlying byte representation of a simple text string. This is useful for understanding how your database stores characters.
Query:
SELECT
RAWTOHEX('Hello') AS "Hello_in_Hex"
FROM DUAL;
Result: (The result is the hexadecimal ASCII/UTF-8 code for each letter: H=48, e=65, l=6C, l=6C, o=6F)
Hello_in_Hex
------------
48656C6C6F
