3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

COBOLでサイン付数値型(S9)から文字型(X)へ符号付き数値をMOVEする

Last updated at Posted at 2019-11-07

前書き

前職でコボラーしてたときの同僚に質問されたら少し詰まったのでメモ書き

やりたいこと

サイン付数値(S9)を符号付けたまま文字型(X)にMOVEしたい。
S9 SIGN IS LEADING SEPARATE CHARACTERで定義して出力すれば同じでしょ?となるのですが、
値がゼロだったら、同じ場所に特定の文字列を出したいとかCSVデータなんかではそんな要件がよくあります。

結論から

S9 SIGN IS LEADING SEPARATE CHARACTERで定義した変数をREDEFINE句経由でXに代入することで実現できました。

S9->S9SC->X
IDENTIFICATION DIVISION.
program-id. teeeest.
DATA DIVISION.
WORKING-STORAGE SECTION.
77 int PIC S9(8) VALUE -12345.
01 disp_rec.
  02 d PIC x(9).
  02 cc REDEFINES d.
    03 eee PIC s9(8) SIGN IS LEADING SEPARATE CHARACTER.
PROCEDURE DIVISION.
    MOVE int TO eee
    DISPLAY "d:" d
EXIT.
Output
d:-00012345

なぜかS9 SIGN IS LEADING SEPARATE CHARACTERをXタイプの変数にMOVEするのだと符号が外れてしまう。
理由の追求はめんどいのでしない(笑)
この辺の情報はネット上には全然なくて困った;

IDENTIFICATION DIVISION.
program-id. teeeest.
DATA DIVISION.
WORKING-STORAGE SECTION.
77 int PIC S9(8) VALUE -12345.
77 ints PIC S9(8) SIGN IS LEADING SEPARATE CHARACTER.
77 str PIC X(9).
01 disp_rec.
  02 d PIC x(9).
  02 cc REDEFINES d.
    03 eee PIC s9(8) SIGN IS LEADING SEPARATE CHARACTER.
PROCEDURE DIVISION.
    MOVE int TO ints
    MOVE ints TO str
    MOVE int TO eee
    DISPLAY "str:" str
    DISPLAY "d  :" d
EXIT.
Output
str:00012345 
d  :-00012345
3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?