|
ASCII vs EBCDIC
Does OS/400 support an ASCII character set?
OS/400 is EBCDIC-based. ASCII-EBCDIC conversion is one of the considerations for choosing the target environment for a port. The OS/400 PASE environment provides an ASCII runtime environment for applications. The ILE environment generally uses EBCDIC. Both environments provide APIs that will convert data at runtime. Although OS/400 operates internally using primarily EBCDIC, there is nothing to prevent applications from maintaining and using ASCII data. Data in stream files can be stored in any code page.
Choosing which environment is right for your application depends on issues such as the data expectations of the application, the amount of porting resource that is required and available, and whether performing a data conversion is acceptable.
The OS/400 ILE C runtime functions require input to be in EBCDIC to function correctly. Almost all UNIX and PC applications operate on data stored in ASCII. This presents a problem if, for instance, the contents of an ASCII file are to be displayed on the screen of an OS/400 ILE application, since it needs to be in EBCDIC to go through the C runtime. The ASCII C/C++ Runtime can be used to aid in porting application to the ILE environment.
How can I change the code page of a file?
If you have a file whose code page tag is not correct for the data inside (this can happen when transferring files via FTP), you can quickly retag the file. Use the Change Attribute CL command. This scenario best applies to stream files (files stored somewhere under root ( ' / ' ), but not files store in /QSYS.lib file system. Here is an example:
CHGATR OBJ('/home/quigg/hello.txt') ATR(*CCSID) VALUE(819)
How can OS/400 support ASCII workstations if it is an EBCDIC system?
OS/400 uses workstation controls that convert the data stream from ASCII to EBCDIC before any data is processed by the application.
How can data be converted between ASCII and EBCDIC?
Two related methods are outlined:
1. To convert strings to a different CCSID than the CCSID of the source, you use #Pragma convert. This preprocessor directive exists in ILE C, starting from V2R3.
To use it, you specify #Pragma convert (ccsid) anywhere in the primary source file, where ccsid is the Coded Character Set ID (ASCII or EBCDIC) that you want your strings to be converted to. From that point on, literals, graphic characters and escape sequences will be converted to the specified code page, until the end of the source file or until #Pragma convert (0) is encountered.
This directive takes effect for the compilation unit, therefore it does not affect externally called programs or bound modules/srvpgms. It will affect all strings in the primary file and secondary (#include) file from the point where #pragma convert is encountered until the end of the compilation unit, or until #Pragma convert (0) to turn it off.
Note that placing #pragma convert(xx) around a macro, as below, is NOT CORRECT. The macro is expanded with the text characters, 'A' for example, later in the source code, where the #pragma does not cover them.
#pragma convert(819)
#define ascii_to_bin(c) ((c)>='a'?(c-59):(c)>='A'?((c)-53):(c)-'.')
#define bin_to_ascii(c) ((c)>=38?((c)-38+'a'):(c)>=12?((c)-12+'A'):(c)+'.')
#pragma convert(0)
You will need to change the above lines to...
#define ascii_to_bin(c) ((c)>=0x61?(c-59):(c)>=0x41?((c)-53):(c)-0x2e)
#define bin_to_ascii(c) ((c)>=38?((c)-38+0x61):(c)>=12?((c)-12+0x41):(c)+0x2e)
See the ILE C/400 Programmer's Reference for more information.
2. To convert from ASCII to EBCDIC, you can use the system API QDCXLATE or National Language Support (NLS) API iconv(). There are three functions in iconv family, iconv_open(), iconv() and iconv_close(). iconv_open() performs the necessary initialization to convert character encoding from the source CCSID to the target CCSID as specified with the function and returns a conversion descriptor. This conversion descriptor is used in iconv() to convert an input buffer in source CCSID to an output buffer in target CCSID. After conversion the converion descriptor should be closed using iconv_close() API. You can download an example on use of iconv functions to convert from one CCSID to another CCSID.
What is the correct value for the #pragma convert (ccsid) if I desire character strings to be compiled as ASCII in ILE C?
OS/400 does not fully support CCSID 367, for 7-bit ASCII. You can, however, use CCSID 819, (ISO 8859-1 Common use default internet code), which is 7-bit ASCII compatible. You can find more information in the books iSeries International Application Development, SC41-3603 (V4R2), the Appendix for Code Pages. or online at Code Page Maps.
[BACK]
|