Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

assign x type data to xstring type issue

Former Member
0 Likes
1,672

hi

how assign x type to xstring.

and get actual length.

example:

data: len type i,

x1(300) type x,

xs type xstring.

case 1:

xs = '2222'.

len = xstrlen( xs ).

we will get len value is 2 which result that i want to get.

case 2.

x1 = '2222'.

len = xstrlen( x1 ).

we will get len value is 300.

xs = x1.

len = xstrlen( xs ).

we will get len value is 300.

i know in var x1 will fill with '00' from left to right. but i want to know actual length not definition lenth.

thank you very much advance.

5 REPLIES 5
Read only

Former Member
0 Likes
1,091

hi,

Use describe statement i.e,


data : len type i. 

DESCRIBE FIELD x1 length len.

Read only

Former Member
0 Likes
1,091

xstrlen will return the actual lenghth only in case of hexstrings.

For x type variables with fixed length xstrlen will return the defined lenghth always.

Try using strlen instead of xstrlen for such cases to get correct occupied lenght. In 4.6 it works. I do not know what release you are on.

Read only

Former Member
0 Likes
1,091

thank you!

release is 6.4!

replace string type to xstring type and get length is definition length yet.

Read only

Former Member
0 Likes
1,091

Hi xiaobing xue,

There is no Function Module in SAP that actually removes the Zero's after the numbers ('222200000000'). CONVERSION_EXIT FM's are there to remove Leading Zero's and add Leading Zero's. So I think you need to manually proceed and calculate the length. Try the following code and you'll get the desired output.

DATA: len TYPE i,

x1(300) TYPE x,

xs TYPE xstring,

xs1 TYPE xstring.

DATA: ch1 TYPE char255,

ch2 TYPE char255.

DATA: l1 TYPE i,

l2 TYPE i.

xs = '2222'.

len = XSTRLEN( xs ).

WRITE:/ 'First Length Calculation', len.

CLEAR: len.

x1 = '2222'.

len = XSTRLEN( x1 ).

DO len TIMES.

IF x1+l2(1) IS NOT INITIAL.

MOVE x1+l2(1) TO ch2.

CONCATENATE ch1 ch2 INTO ch1.

l2 = l2 + 1.

ELSE.

EXIT.

ENDIF.

ENDDO.

MOVE ch1 TO xs1.

len = XSTRLEN( xs1 ).

WRITE:/ 'Second Length Calculation', len.

CLEAR: len, ch1, ch2, l2.

xs = x1.

len = XSTRLEN( xs ).

DO len TIMES.

IF xs+l2(1) NE '00'.

MOVE xs+l2(1) TO ch2.

CONCATENATE ch1 ch2 INTO ch1.

l2 = l2 + 1.

ELSE.

EXIT.

ENDIF.

ENDDO.

MOVE ch1 TO xs1.

len = XSTRLEN( xs1 ).

WRITE:/ 'Third Length Calculation', len.

Hope that this solves your problem. Reward accordingly.

Thanks and Regards,

Maddineni Bharath.

Read only

Former Member
0 Likes
1,091

thank you

this

CLEAR: len, ch1, ch2, l2.

xs = x1.

len = XSTRLEN( xs ).

DO len TIMES.

IF xs+l2(1) NE '00'.

MOVE xs+l2(1) TO ch2.

CONCATENATE ch1 ch2 INTO ch1.

l2 = l2 + 1.

ELSE.

EXIT.

ENDIF.

ENDDO.

MOVE ch1 TO xs1.

len = XSTRLEN( xs1 ).

WRITE:/ 'Third Length Calculation', len.

is i want!

thank you !