SAP NetWeaver AS ABAP Release 740, ©Copyright 2014 SAP AG. All rights reserved.
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, (b, s) →Conversion of Integer Numbers to Bytes
This example demonstrates the conversion of integers into byte fields and strings.
Source Code
REPORT demo_int_to_hex.
CLASS demo DEFINITION.
PUBLIC SECTION.
CLASS-METHODS main.
PRIVATE SECTION.
TYPES:
BEGIN OF line,
int1 TYPE c LENGTH 60,
int2 TYPE c LENGTH 60,
int4 TYPE c LENGTH 60,
* int8 TYPE c LENGTH 60,
END OF line.
CLASS-DATA output TYPE TABLE OF line.
CLASS-METHODS write_output IMPORTING VALUE(idx) TYPE i
VALUE(col) TYPE i
text TYPE clike.
ENDCLASS.
CLASS demo IMPLEMENTATION.
METHOD main.
DATA: decf TYPE decfloat34,
int1 TYPE int1,
int2 TYPE int2,
int4 TYPE int4,
* int8 TYPE int8,
indx TYPE sy-index,
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.
decf = 2 ** ( sy-index - 1 ) - 1 .
int1 = decf.
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.
indx = 16 - sy-index.
decf = - 2 ** ( indx ) + 1.
int2 = decf.
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.
decf = 2 ** ( sy-index - 1 ) - 1 .
int2 = decf.
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.
indx = 32 - sy-index.
decf = - 2 ** ( indx ) + 1.
int4 = decf.
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.
decf = 2 ** ( sy-index - 1 ) - 1 .
int4 = decf.
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.
* indx = 64 - sy-index.
* decf = - 2 ** ( indx ) + 1.
* int8 = decf.
* xstr = int8.
* xfld = int8.
* write_output(
* idx = sy-index
* col = 4
* text = |{ int8 WIDTH = 12 ALIGN = RIGHT } {
* xfld ALIGN = LEFT } { xstr ALIGN = LEFT }| ).
* ENDDO.
* DO 64 TIMES.
* decf = 2 ** ( sy-index - 1 ) - 1 .
* int8 = decf.
* xstr = int8.
* xfld = int8.
* write_output(
* idx = sy-index + 63
* col = 4
* text = |{ int8 WIDTH = 12 ALIGN = RIGHT } {
* xfld ALIGN = LEFT } { xstr ALIGN = LEFT }| ).
* ENDDO.
cl_demo_output=>display( output ).
ENDMETHOD.
METHOD write_output.
ASSIGN output[ idx ] TO FIELD-SYMBOL(<line>).
IF sy-subrc <> 0.
DO.
APPEND INITIAL LINE TO output ASSIGNING <line>.
IF sy-tabix = idx.
EXIT.
ENDIF.
ENDDO.
ENDIF.
ASSIGN COMPONENT col OF STRUCTURE <line> TO FIELD-SYMBOL(<col>).
<col> = text.
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
demo=>main( ).
Description
This program takes numbers of the data types b, s, and i,
that cover the entire value range and assigns them to byte fields of the lengths 1, 2, 4, and 8 and to 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.