Skip to content

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

Conversion of Integer Numbers to Bytes

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

Other versions: 7.31 | 7.40 | 7.54

Source Code

    DATA: int1 TYPE int1,
          int2 TYPE int2,
          int4 TYPE int4,
          int8 TYPE int8,
          xstr TYPE xstring,
          xfld TYPE x LENGTH 8.
    cl_demo_output=>begin_section(
      `Conversion of Integers to Byte Fields and Byte Strings` ).
    DO 9 TIMES.
      int1 = ipow( base = 2 exp = sy-index - 1 ) - 1 .
      xstr = int1.
      xfld = int1.
      write_output(
        idx = sy-index
        col = 1
        text = |{ int1 WIDTH = 4 ALIGN = RIGHT } {
                  xfld ALIGN = LEFT } { xstr ALIGN = LEFT }| ).
    ENDDO.
    DO 15 TIMES.
      int2 = - ipow( base = 2 exp = 16 - sy-index ) + 1 .
      xstr = int2.
      xfld = int2.
      write_output(
        idx = sy-index
        col = 2
        text = |{ int2 WIDTH = 7 ALIGN = RIGHT } {
                  xfld ALIGN = LEFT } { xstr ALIGN = LEFT }| ).
    ENDDO.
    DO 16 TIMES.
      int2 = ipow( base = 2 exp = sy-index - 1 ) - 1 .
      xstr = int2.
      xfld = int2.
      write_output(
        idx = sy-index + 15
        col = 2
        text = |{ int2 WIDTH = 7 ALIGN = RIGHT } {
                  xfld ALIGN = LEFT } { xstr ALIGN = LEFT }| ).
    ENDDO.
    DO 31 TIMES.
      int4 = - CONV decfloat34(
                 ipow( base = 2 exp = 32 - sy-index ) ) + 1 .
      xstr = int4.
      xfld = int4.
      write_output(
        idx = sy-index
        col = 3
        text = |{ int4 WIDTH = 12 ALIGN = RIGHT } {
                  xfld ALIGN = LEFT } { xstr ALIGN = LEFT }| ).
    ENDDO.
    DO 32 TIMES.
      int4 = CONV decfloat34(
               ipow( base = 2 exp = sy-index - 1 )  ) - 1 .
      xstr = int4.
      xfld = int4.
      write_output(
        idx = sy-index + 31
        col = 3
        text = |{ int4 WIDTH = 12 ALIGN = RIGHT } {
                  xfld ALIGN = LEFT } { xstr ALIGN = LEFT }| ).
    ENDDO.
    DO 63 TIMES.
      int8 = - CONV decfloat34(
                 ipow( base = 2 exp = 64 - sy-index ) ) + 1 .
      xstr = int8.
      xfld = int8.
      write_output(
        idx = sy-index
        col = 4
        text = |{ int8 WIDTH = 21 ALIGN = RIGHT } {
                  xfld ALIGN = LEFT } { xstr ALIGN = LEFT }| ).
    ENDDO.
    DO 64 TIMES.
      int8 = CONV decfloat34(
                ipow( base = 2 exp = sy-index - 1 ) ) - 1 .
      xstr = int8.
      xfld = int8.
      write_output(
        idx = sy-index + 63
        col = 4
        text = |{ int8 WIDTH = 21 ALIGN = RIGHT } {
                  xfld ALIGN = LEFT } { xstr ALIGN = LEFT }| ).
    ENDDO.
    cl_demo_output=>display( output ).

Description

The program assigns numbers of the data types b, s, and i and int8, which cover the entire value range, to byte fields with the lengths 1, 2, 4, and 8 as well as to a byte string and displays them.

In part, the calculation type decfloat34 is forced to calculate numbers using powers of two. This prevents overflows when assigning the results of the function ipow to subtotals of the calculation type i or int8.

This example highlights the different lengths that can be produced 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.