0

I'm using Oracle 11g on Windows 7 with national character set equal to AL16UTF16.

I'm trying to write 3 cyrillic letters with the following PL/SQL code:

declare
  s nvarchar2(3):=nchr(1040) || nchr(1041) || nchr(1042);
  fileHandler UTL_FILE.FILE_TYPE;
begin
  fileHandler := UTL_FILE.FOPEN_NCHAR('TEMP', 'test.txt', 'W');
  UTL_FILE.PUT_NCHAR(fileHandler,s);
  UTL_FILE.FCLOSE(fileHandler);
end;

The resulting file is 5 bytes long. If I open the file in Notepad++ I can see the following: DLE DC1 DC2 with new line at the end (character 13+character 10). What is wrong with the code above?

user416472
  • 104
  • 1
  • 8

1 Answers1

0

There is a lot of magic with line terminations in UTL_FILE in text mode. You may experiance a different behaviour if you will be using some other OS.

to avoid it, you may use binary mode with conversion

fileHandler := UTL_FILE.FOPEN('TEMP', 'test.txt', 'wb');
UTL_FILE.PUT_RAW (fileHandler, utl_raw.cast_to_raw(s));
vav
  • 4,584
  • 2
  • 19
  • 39
  • The change did not solve the problem. With the first version of the program the bytes written are: 16 17 18 13 10 The second version generates the following bytes: 0 16 0 17 0 18 But, what the desired result if Unicode should be: 254 255 4 16 4 17 4 18 – user416472 May 17 '14 at 03:35
  • That's true. I gave solution only for the line terminator. For correct encoding you may play with CONVERT and HEXTORAW functions. – vav May 17 '14 at 18:02