在 Oracle 中从十六进制编码的 CLOB 转换为 BLOB
Convert from hex-encoded CLOB to BLOB in Oracle
我有一些以十六进制形式存储在 CLOB 中的大型二进制内容,我想将其转换为 BLOB,其中十六进制代码是实际的二进制字节编码:
DECLARE
-- This would be my 8 byte hex-encoded binary content. Real content is much bigger
c CLOB := 'cafebabe12345678';
b BLOB;
BEGIN
-- Need the implementation of this function
b := hex_to_blob(c);
END;
/
在 Oracle 中使用 PL/SQL 最简单的方法是什么?
所需的功能可能如下所示:
CREATE OR REPLACE
FUNCTION hex_to_blob (hex CLOB) RETURN BLOB IS
b BLOB := NULL;
s VARCHAR2(4000 CHAR) := NULL;
l NUMBER := 4000;
BEGIN
IF hex IS NOT NULL THEN
dbms_lob.createtemporary(b, FALSE);
FOR i IN 0 .. LENGTH(hex) / 4000 LOOP
dbms_lob.read(hex, l, i * 4000 + 1, s);
dbms_lob.append(b, to_blob(hextoraw(s)));
END LOOP;
END IF;
RETURN b;
END hex_to_blob;
我有一些以十六进制形式存储在 CLOB 中的大型二进制内容,我想将其转换为 BLOB,其中十六进制代码是实际的二进制字节编码:
DECLARE
-- This would be my 8 byte hex-encoded binary content. Real content is much bigger
c CLOB := 'cafebabe12345678';
b BLOB;
BEGIN
-- Need the implementation of this function
b := hex_to_blob(c);
END;
/
在 Oracle 中使用 PL/SQL 最简单的方法是什么?
所需的功能可能如下所示:
CREATE OR REPLACE
FUNCTION hex_to_blob (hex CLOB) RETURN BLOB IS
b BLOB := NULL;
s VARCHAR2(4000 CHAR) := NULL;
l NUMBER := 4000;
BEGIN
IF hex IS NOT NULL THEN
dbms_lob.createtemporary(b, FALSE);
FOR i IN 0 .. LENGTH(hex) / 4000 LOOP
dbms_lob.read(hex, l, i * 4000 + 1, s);
dbms_lob.append(b, to_blob(hextoraw(s)));
END LOOP;
END IF;
RETURN b;
END hex_to_blob;