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.
Other versions: 7.31 | 7.40 | 7.54
Source Code
DATA: decf TYPE decfloat34,
int1 TYPE int1,
int2 TYPE int2,
int4 TYPE int4,
<span class="blue">* int8 TYPE int8,</span>
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.
<span class="blue">* DO 63 TIMES.</span>
<span class="blue">* indx = 64 - sy-index.</span>
<span class="blue">* decf = - 2 ** ( indx ) + 1.</span>
<span class="blue">* int8 = decf.</span>
<span class="blue">* xstr = int8.</span>
<span class="blue">* xfld = int8.</span>
<span class="blue">* write_output(</span>
<span class="blue">* idx = sy-index</span>
<span class="blue">* col = 4</span>
<span class="blue">* text = |{ int8 WIDTH = 12 ALIGN = RIGHT } {</span>
<span class="blue">* xfld ALIGN = LEFT } { xstr ALIGN = LEFT }| ).</span>
<span class="blue">* ENDDO.</span>
<span class="blue">* DO 64 TIMES.</span>
<span class="blue">* decf = 2 ** ( sy-index - 1 ) - 1 .</span>
<span class="blue">* int8 = decf.</span>
<span class="blue">* xstr = int8.</span>
<span class="blue">* xfld = int8.</span>
<span class="blue">* write_output(</span>
<span class="blue">* idx = sy-index + 63</span>
<span class="blue">* col = 4</span>
<span class="blue">* text = |{ int8 WIDTH = 12 ALIGN = RIGHT } {</span>
<span class="blue">* xfld ALIGN = LEFT } { xstr ALIGN = LEFT }| ).</span>
<span class="blue">* ENDDO.</span>
cl_demo_output=>display( output ).
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.