MainframeMaster

COBOL Tutorial

COBOL UCS-2 - Quick Reference

Progress0 of 0 lessons

Overview

UCS-2 is a fixed-width 16-bit Unicode encoding covering the Basic Multilingual Plane. In COBOL, NATIONAL data (PIC N) is commonly stored as UCS-2 or UTF-16 depending on the compiler.

Key Concepts

  • NATIONAL - PIC N defines national-character strings
  • Conversion - DISPLAY-OF, NATIONAL-OF convert between forms
  • Files - Consider CODE-SET, byte order, and interoperability

Syntax and Usage

NATIONAL Data and Conversion

cobol
1
2
3
4
5
6
7
8
9
10
* NATIONAL data and conversion DATA DIVISION. WORKING-STORAGE SECTION. 01 WIDE-NAME PIC N(30). 01 DISP-NAME PIC X(60). PROCEDURE DIVISION. MOVE FUNCTION NATIONAL-OF("Jürgen Müller") TO WIDE-NAME MOVE FUNCTION DISPLAY-OF(WIDE-NAME) TO DISP-NAME DISPLAY "Name: " DISP-NAME STOP RUN.

File CODE-SET Considerations

cobol
1
2
3
4
5
6
7
8
* Illustrative: writing NATIONAL data (compiler-specific details omitted) FD OUT-FILE. 01 OUT-REC. 05 OUT-NAME PIC N(30). PROCEDURE DIVISION. MOVE FUNCTION NATIONAL-OF("Álvaro") TO OUT-NAME * Ensure file/code-page settings match NATIONAL representation * Some systems require CODE-SET and explicit conversions.

Best Practices

  • Know your runtime - Verify whether NATIONAL is UCS-2 or UTF-16
  • Validate conversions - Test DISPLAY-OF/NATIONAL-OF with target encodings
  • Document code pages - Record code-set assumptions for files and DBs

UCS-2 Quick Reference

AspectDescriptionExample
NATIONALPIC NPIC N(30)
Convert to NATIONALFUNCTION NATIONAL-OFNATIONAL-OF("Café")
Convert to DISPLAYFUNCTION DISPLAY-OFDISPLAY-OF(WIDE-NAME)

Test Your Knowledge

1. What is UCS-2?

  • A variable-length Unicode encoding
  • A fixed-width 16-bit Unicode encoding (Basic Multilingual Plane)
  • An EBCDIC variant
  • A compression algorithm

2. Which COBOL items commonly map to UCS-2?

  • USAGE DISPLAY numeric items
  • NATIONAL data items (PIC N)
  • COMP-3 items
  • INDEX items

3. Which functions convert between DISPLAY and NATIONAL text?

  • FUNCTION NUMVAL and NUMVAL-C
  • FUNCTION DISPLAY-OF and NATIONAL-OF
  • FUNCTION REM and MOD
  • FUNCTION RANDOM and CURRENT-DATE

4. What should you consider when writing UCS-2 data to files?

  • Byte order and code-set negotiation
  • Only record length
  • File block size only
  • Nothing, it is always transparent

5. Which is true of UCS-2 vs UTF-16?

  • They are identical for all characters
  • UCS-2 is fixed-width and does not support characters beyond the BMP; UTF-16 can use surrogate pairs
  • UCS-2 is variable-length
  • UTF-16 is limited to 8-bit values

Frequently Asked Questions