How force specific charset in write work file

Hi, first, sorry for poor english.

How force specific charset in write work file in a subprogram? I need force ISO-8859-1 for a string in the moment of write work file, it’s possible? If not, it’s possible encode accents and special characters for HTML?

ISO-8859-1 is a SBCS codepage, so why would you need to translate something to it, assuming your Natural session already runs with the correct locale ?

When you “force” something into a specific codepage from another there is always a potential risk that you end up with some characters that didn’t translate correctly.

Do you know what CP the data you need to "force to 8859-1 is in ?

General Question: Is your Natural running on MF (with or without ICU) or LUW?

In general you can use the MOVE ENCODED statement for code-page/charset translation.

Best practice, in my opinion, is that the receiving variable is a binary dynamic.

Then you can DEFINE WORK with “UNFORMATTED” and the binary variable will be written unchanged to the work file.

If you want to generate e.g. an HTML/XML page, have a look at the variable *CODEPAGE which returns the current settings of the Natural System (on LUW, on MF this variable is by be empty!)

For HTML translation you can use examine replace with two arrays - the one defines as A1 with the character you want to translate and the other one as A8 with your HTML entity names.

Consider using a function e.g. named XML then you can add a translation for anywhere.

MOVE ENCODED #XML_A TO #XML_B CODEPAGE 'utf-8'
DEFINE WORK FILE 12 #FILE_PATH TYPE "UNFORMATTED"
WORK FILE 12 VARIABLE #XML_B
CLOSE WORK 12
COMPRESS '<!DOCTYPE html><html><head>'
  '<meta charset="' *CODEPAGE '">'
 INTO #HTML_A LEAVING NO
COMPRESS    '<?xml version="1.0" encoding="' *CODEPAGE '"?>'
  INTO #XML_A LEAVING NO
DEFINE PROTOTYPE  XML RETURNS (A) DYNAMIC
DEFINE DATA PARAMETER
1 P00H (A) DYNAMIC BY VALUE
END-DEFINE
END-PROTOTYPE
*
DEFINE FUNCTION  XML RETURNS (A) DYNAMIC
DEFINE DATA PARAMETER
1 P00H (A) DYNAMIC BY VALUE
LOCAL
1 ##ASCII_XML (1:3)
  2 ASCII (A1)
  2 XM   (A8) /* XML is reserved by the function name
END-DEFINE
*
* ---- Ampersand
##ASCII_XML.ASCII(001) := "&"        ##ASCII_XML.XM(001) := '&'
* ---- Greater than
##ASCII_XML.ASCII(002) := ">"        ##ASCII_XML.XM(002) := '>'
* ---- Less than
##ASCII_XML.ASCII(003) := "<"        ##ASCII_XML.XM(003) := '<'


* ------ MAIN PROGRAM --------------------------------------------
XML := P00H
*
EXAMINE XML FOR ##ASCII_XML.ASCII(*) REPLACE WITH ##ASCII_XML.XM(*)
*
END-FUNCTION
END
COMPRESS #HTML '<p>' XML(< #MYDATABASEFIELD >) '</p>'   H'0D0A'
INTO #HTML LEAVING NO

The generated file is read by a Java application. The generated file is a .txt.

The subprogram is written “ÁÉÍÓÚǪº” , but int the file was written “Ý?²ä§CÞÑ??”. I’ve tried to read in all possible charsets in the Java application(CP037, CP1252, UTF-8, ISO-8859-1, UTF16…) but without success. So force a charset was the alternative.

It’s perfect, thank you!