Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

Hexadecimal to Binary

586006Jun 7 2012 — edited Jun 8 2012
Hi,

I am using this query to convert hexadecimal data to binary data which I got. But this query is trimming the leading 0 if it is there whereas I want to show all the data including leading 0 if it should be there. Please suggest.

SELECT utl_raw.cast_to_varchar2(utl_raw.reverse(utl_raw.cast_to_raw(to_char(str)))) AS RESULT
FROM
(WITH DATA AS
(SELECT SUM( num * POWER(base, rn -1) ) base_10_num
FROM
(SELECT instr(num_str, upper(doc.extract('/X/text()').getStringVal()))-1
as num, rownum rn, a.base
FROM
(SELECT '0123456789ABCDEF' as num_str,
:base as base,
utl_raw.cast_to_varchar2(utl_raw.reverse(utl_raw.cast_to_raw(to_char(:input))))
as input FROM DUAL) a,
TABLE(xmlSequence(extract(XMLType('<DOC>'||
REGEXP_REPLACE(a.input,'(.)','<X>\1</X>')||'</DOC>'),'/DOC/X'))) doc
)
)
SELECT max(replace(sys_connect_by_path(sign(bitand(base_10_num, power(2,LEVEL-1))),','),','))
AS str
FROM (SELECT base_10_num FROM DATA) a
CONNECT BY power(2,LEVEL - 1)<= base_10_num
);
This post has been answered by AlbertoFaenza on Jun 7 2012
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 6 2012
Added on Jun 7 2012
25 comments
12,622 views