Converting Between Character Sets

You can use PeopleCode file functions to convert files or text strings from one supported PeopleSoft character set to another supported PeopleSoft character set.

PeopleCode operations such as GetFile, GetTempFile, Open, ReadLine, and WriteLine automatically account for the file encoding. Therefore, you can:

  • Convert from a Unicode character set to a non-Unicode character set.

  • Convert from a non-Unicode character set to a Unicode character set.

  • Automatically handle the Unicode BOM if it is present or needs to be written.

When using a character set such as UCS2 or UTF8BOM, the BOM is added at the beginning of the file contents when using WriteLine. The BOM is skipped when read by the ReadLine PeopleCode function, and not interpreted as a text character. Since the BOM is recognised as meta data and not part of the file's text the BOM is not added to file contents when writing in a non-Unicode character set with the WriteLine PeopleCode function.

In the following example PeopleCode program, the FileEncodingConversion function handles converting files from one supported character set to another. In the body of the program, the function is called to convert from the UTF8BOM character set to the UCS2 character set.:

REM this function, FileEncodingConversion() converts character encoding of 
input file to another of output file.
REM an example of how to call the function is at the end of this file.

Local File &InputFile, &OutputFile;
Local string &InputDirFile, &OutputDirFile, &InputFilename, &OutputFilename;
Local string &sDirSep, &LogLine;
Local array of string &FIleEncoding;
Local boolean &ret;

Function FileEncodingConversion(&InputEncoding, &InputDirectoryFile, 
&OutputEncoding, &OutputDirectoryFile) Returns boolean

   &InputFile = GetFile(&InputDirectoryFile, "R", &InputEncoding, 
%FilePath_Absolute);
   &OutputFile = GetFile(&OutputDirectoryFile, "W", &OutputEncoding, 
%FilePath_Absolute);

   If &InputFile.IsOpen And
&OutputFile.IsOpen Then

      While (&InputFile.readline(&LogLine))
&OutputFile.Writeline(&LogLine);
      End-While;

      &InputFile.Close();
      &OutputFile.Close();
      
      Return True;
      
   Else
      If &InputFile = Null Then
         WinMessage("Error: PeopleCode: File Encoding I/O: " | "Failed to 
open: " | &InputFile.Name);
      Else
         If &OutputFile = Null Then
            WinMessage("Error: PeopleCode: File Encoding I/O: " | "Failed
to open:" | &OutputFile.Name);
         End-If;
      End-If;
      Return False;
   End-If;

End-Function;

/*-----------------------------------------------------------------------*/
/*  Function IsUnix                                                      */
/* check if OS = Unix                                                    */
/*-----------------------------------------------------------------------*/
Function IsUnix Returns boolean
   &DummyFile = GetFile("/bin/sh", "E", %FilePath_Absolute);
   If &DummyFile.IsOpen Then;
      &DummyFile.Close();
      Return True;
   Else;
      Return False;
   End-If;
End-Function;

REM test the function above;
&FIleEncoding = CreateArray("UTF8BOM", "UCS2", "SJIS", "GB18030", "UTF8", "a", "u");

REM WinMessage("ret: " | &ret);

If IsUnix() Then
   /* for UNIX */
   &ret = FileEncodingConversion(&FIleEncoding [1], "/home/FS_" | 
&FIleEncoding [1] | ".txt", &FIleEncoding [2], "/home/BEFORE/FS_PCode_" | &FIleEncoding [1] | "_to_" | &FIleEncoding [2] | ".txt");
Else
   /* for WINDWOS */
   &ret = FileEncodingConversion(&FIleEncoding [1], "D:\TMP\FS_" | 
&FIleEncoding [1] | ".TXT", &FIleEncoding [2], "D:\TMP\AFTER\FS_PCode_" | 
&FIleEncoding [1] | "_to_" | &FIleEncoding [2] | ".TXT");
End-If