Skip to Main Content

Breadcrumb

Question and Answer

Tom Kyte

Thanks for the question, KK.

Asked: October 31, 2000 - 3:27 am UTC

Last updated: July 07, 2005 - 12:58 pm UTC

Version: 8.1.6

Viewed 1000+ times

You Asked

I've to read a file which contains binary data,
how can I convert the data into the hexidecimal value?

For example:null->00

Moreover, can Oracle read null character from a file?

and Tom said...

You would use BFILES to read the data into a RAW variable. Once in a RAW variable -- a simple assignment of that data to a VARCHAR variable will assign a HEX string to the varchar.

for example, I wrote a small C program:

void main()
{
int c;

for( c = 0; c <256; putchar(c++) );
}


and ran it:

$ test > test.dat

And then the plsql block:


ops$tkyte@ORA8I.WORLD> create or replace directory
2 my_files as '/export/home/tkyte/public_html';

Directory created.

ops$tkyte@ORA8I.WORLD>
ops$tkyte@ORA8I.WORLD> declare
2 l_raw raw(4000);
3 l_bfile bfile default bfilename( 'MY_FILES', 'test.dat' );
4 begin
5 dbms_lob.fileopen( l_bfile );
6 l_raw := dbms_lob.substr( l_bfile,4000,1 );
7 dbms_lob.fileclose( l_bfile );
8
9 for i in 1 .. utl_raw.length(l_raw)
10 loop
11 dbms_output.put_line( 'Byte ' || i || ' = ' ||
12 utl_raw.substr(l_raw,i,1) );
13 end loop;
14 end;
15 /
Byte 1 = 00
Byte 2 = 01
Byte 3 = 02
Byte 4 = 03
Byte 5 = 04
Byte 6 = 05
Byte 7 = 06
Byte 8 = 07
Byte 9 = 08
Byte 10 = 09
Byte 11 = 0A
....
Byte 254 = FD
Byte 255 = FE
Byte 256 = FF

PL/SQL procedure successfully completed.

ops$tkyte@ORA8I.WORLD>

UTL_RAW might not be installed on your system -- it comes with
Oracle7.1.6 and up. To install if you do not have it:

o cd $ORACLE_HOME/rdbms/admin
o connect as SYS or INTERNAL only using SVRMGRL
o @utlraw
o @prvtrawb.plb






Rating

  (9 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

convert hex file to binary/raw file

A reader, November 11, 2003 - 3:48 pm UTC

Hi

There was this developer in our company who wrote some perl program to convert jpeg files into hexadecimal files then using utl_file.get_line concatenate line by line and store as a raw variable and store as BLOB (using dbms_lob.write) in the database. It stinks but I am not allowed to change the whole process, the perl program is untouchble.

So my question is, is there a way to convert in a more efficient way a hex file into binary/raw format instead of concatenating line after line...?



Tom Kyte
November 12, 2003 - 6:39 am UTC

you don't need to concat, you can

loop
read varchar2 string
assign to raw (will auto-convert hex to raw)
dbms_lob.writeAppend


reset raw variable to zero

A reader, November 11, 2003 - 6:13 pm UTC

Hi

If a variable is of type RAW and it's value is increased inside a loop is there anyway to reset it to zero after X loop iterations?

Tom Kyte
November 12, 2003 - 6:56 am UTC

huh?

what do you mean by "zero" with regards to a "raw"?

Fun with Raws

Dan Clamage, November 12, 2003 - 2:47 pm UTC

declare
v_raw raw(4000);
begin
for i in 0 .. 255 loop
v_raw := v_raw || utl_raw.cast_to_raw(chr(i));
-- equivalent expression without || operator
-- v_raw := utl_raw.concat(v_raw, utl_raw.cast_to_raw(chr(i)));
end loop;
for i in 1 .. utl_raw.length(v_raw) loop
dbms_output.put_line('raw(' || i || ')=[' ||
utl_raw.substr(v_raw, i, 1) || ']');
end loop;
end;
/
raw(1)=[00]
raw(2)=[01]
raw(3)=[02]
...
raw(254)=[FD]
raw(255)=[FE]
raw(256)=[FF]

Hexadecimal

Eva, November 18, 2003 - 5:05 pm UTC

This was somewhat helpful. It gave me some idea.
What I want to do is how I can save my file in .txt
and then covert my string data-characters and numbers
to hexidecimal. I can't seem to find a pl/sql script that
does it or what I need to write this script.

Any guidance would be helpful.

Tom Kyte
November 21, 2003 - 11:09 am UTC



utl_file is the tool for plsql to write files.


numbers can be converted to hex using "to_char( number, 'XXXXXXX' )"

strings via:

hex_string := utl_raw.cast_to_raw( orig_string );

make sure hex_string is 2x as long as orig_string!!!

Conversion HEX to RAW and RAW to hex

Vikas Sharma, November 19, 2003 - 8:56 pm UTC

Hi Tom,

I have a table which store a binary file content(ms word doc file ) in a blob column. The data is inserted by using LOADFROMFILE of dbms_lob.

I am reading the blob in chunks of 57 using dbms_lob.substr which return the raw data. then i writes it to a text file hex. I use the utl_file.put_line to write file. when a raw data is assign to varchar2 variable it automatically get converted to hex. Am I right? So my text file is writen in hex. Pl correct me if worng.

Now i run a utilities available on internet hex2bin.exe and bin2hex.exe to perform conversion. these utility works fine when i convert a doc file to hex using bin2hex.exe and then again hex2bin.exe i get the same back. But does not work well with the file created by using oracle UTL_FILE. and does not create the binary file (doc file).

When i load the same doc file to database and write a hex file using utl_file and compare the hex file create by utl_file and bin2hex.exe utility. They are different.

Why the 2 file are differernt Does the automatic conversion from raw to varchar of utl_file generate different HEX data.

I am not able tounderstand this behaviour.

Actually my requirment is to display the data ie file store in the database on client FORM6i. The only way suggested by oracle metalink, is read from database write a text file(in hex) at client using text_io then run a utility to convert it to binary file back at client so that way your file is transfer from database to client. then you can open it on client using host cammand..

Any kind of help or suggestions will be higly appreciated.

Thanks,

Reagrds

Vikas Sharma




Tom Kyte
November 21, 2003 - 3:34 pm UTC

assign a raw to a string and you have hex, yes.

utl_file has a 32k limit on a line length. you are injecting (and have no way not to) a linefeed in there.

if you can use the web, we can do this trivially with iAS and mod_plsql. a simple URL would retreive the document.

Thanks

vikas Sharma, November 24, 2003 - 7:50 pm UTC

Hi Tom,

Thanks,

Regards

Vikas Sharma

Ram, July 07, 2005 - 9:27 am UTC

Tom,

Could you let mw know how find the NOT value of a HEXADECIMAL value in SQL Query ?

Thanks and regards,
Ram

Tom Kyte
July 07, 2005 - 9:52 am UTC

the "NOT" value?

Ram, July 07, 2005 - 10:25 am UTC

I wanted to find the NOT of HEXADECIMAL value "AAA"
The NOT for Hexadecimal value "AAA" is "FFFFF555".
I need to achieve the same in a SQL Query.

Tom Kyte
July 07, 2005 - 12:58 pm UTC

  1  select to_char(4294967295-to_number('AAA','XXXXXXXX'),'fmXXXXXXXX')
  2*       from dual
ops$tkyte@ORA10G> /
 
TO_CHAR(4
---------
FFFFF555
 

Ram, July 08, 2005 - 2:33 am UTC

Excellent Tom !

Thank you very much !

Best regards,
Ram.



More to Explore

CLOB/BLOB/etc

Complete documentation on Securefiles and Large Objects here