Skip to content

ABAP Keyword Documentation →  ABAP - Reference →  Assignments →  Assignment Rules →  Conversion Rules for Elementary Data Objects →  Numeric Source Fields →  Source Field Type i, (b, s) 

Conversion of Integer Numbers to Bytes

This example demonstrates the conversion of integers into byte fields and strings.

Other versions: 7.31 | 7.40 | 7.54

Source Code

    DATA: int1 TYPE int1,
          int2 TYPE int2,
          int4 TYPE int4,
          indx TYPE sy-index,
          xstr TYPE xstring,
          xfld TYPE x LENGTH 4.
    DO 9 TIMES.
      int1 = 2 ** ( sy-index - 1 ) - 1 .
      xstr = int1.
      xfld = int1.
      WRITE: /(4) int1, xfld, xstr.
    ENDDO.
    BACK.
    DO 15 TIMES.
      indx = 16 - sy-index.
      int2 = - 2 ** ( indx ) + 1.
      xstr = int2.
      xfld = int2.
      WRITE: /20(8) int2, xfld, xstr.
    ENDDO.
    DO 16 TIMES.
      int2 = 2 ** ( sy-index - 1 ) - 1 .
      xstr = int2.
      xfld = int2.
      WRITE: /20(8) int2, xfld, xstr.
    ENDDO.
    BACK.
    DO 31 TIMES.
      indx = 32 - sy-index.
      int4 = - 2 ** ( indx ) + 1.
      xstr = int4.
      xfld = int4.
      WRITE: /50(12) int4, xfld, xstr.
    ENDDO.
    DO 32 TIMES.
      int4 = 2 ** ( sy-index - 1 ) - 1 .
      xstr = int4.
      xfld = int4.
      WRITE: /50(12) int4, xfld, xstr.
    ENDDO.

Description

The program assigns numbers of the data types b, s, and i, which cover the entire value range, a byte field with the length 1 and a byte string and displays them in a classic list.

To calculate the numbers using powers of two, the calculation type decfloat34 is forced as a results field (using a decimal floating point number). If the integer types are used directly as results fields, the calculation type is f.

This example highlights the different lengths that can occur for these assignments to byte strings. It also shows that, in the case of assignments of the type s to x, a field length of 4 bytes is required for negative numbers.