Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trailing space in i to string conversion in ABAP

Tags:

abap

On a SAP system, ABAP version 7.40 SP05, I just encountered a failure in unit tests on string comparison, but both strings should be the same?! Turns out it's not the case, as preceding conversion from i to string seems to produce extra trailing space in one of the strings.

This code bit:

DATA(i) = 111.
DATA(s1) = CONV string( i ).
DATA(s2) = '111'.
DATA(s3) = |111|.

Produces (as seen in debugger):

S1                                      111 3100310031002000    CString{4}
S2                                      111 310031003100    C(3)
S3                                      111 310031003100    CString{3}

The converted one has an extra trailing space. How does this happen and how can I prevent this to happen in i to string conversions? Obviously stuff like this makes me debug for a long time to find what is up (because unless I check the hex values, the debuger does not show that extra space...).

like image 555
dante4d Avatar asked Oct 21 '25 03:10

dante4d


1 Answers

To understand why the space is added in the first place, check the documentation on the default conversion rules that are applied by CONV:

The character "-" is set at the last position for a negative value, and a blank is set for a positive value.

Since you can't use the formatting options of string expressions with the CONV operator, I'd suggest changing the code to use |{ i }| (which might be a good idea for other values as well, since you'll probably need some formatting options when comparing date / time values in unit tests anyway).

like image 190
vwegert Avatar answered Oct 24 '25 10:10

vwegert