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...Casting for integers problem

ged_hurst
Participant
0 Likes
1,322

Hi Guys

Can anyone explain why the following program:

REPORT ZTEST_CASTING.

types mytype2 type i.

data:

smallfield type c LENGTH 5,

smallfield2 type c LENGTH 4 VALUE '1'.

FIELD-SYMBOLS: <fa> type ANY.

ASSIGN smallfield2 to <fa> CASTING type mytype2.

write: / smallfield2, <fa>.

gives the result:

1 3,211,296 ???

Why can't I cast my field symbol to an integer?

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
932

use it like this now it may help

types mytype2 type i.

data:

smallfield type c LENGTH 5,

smallfield2 type c LENGTH 4 VALUE '1'.

FIELD-SYMBOLS: <fa> type c.

ASSIGN smallfield2 to <fa> casting .

write: / smallfield2, <fa>.

7 REPLIES 7
Read only

Former Member
0 Likes
933

use it like this now it may help

types mytype2 type i.

data:

smallfield type c LENGTH 5,

smallfield2 type c LENGTH 4 VALUE '1'.

FIELD-SYMBOLS: <fa> type c.

ASSIGN smallfield2 to <fa> casting .

write: / smallfield2, <fa>.

Read only

0 Likes
932

Thanks Imran. However, I would like the result cast into an integer target field. Why can I not cast an numeric character into an integer field?

Read only

0 Likes
932

Hi Ged,

You should have a look to hexadecimal values in debug :

SMALLFIELD2 -> 0031002000200020
<FA>        -> 00310020

and '0031' is ASCII code for '1' and '0020' for space (' '). Assignement just cut hexadecimal values in place and as integer (see help on built-in types : integer -> 4 bytes) is as long as 2 chars, you obtain the above value.

To obtain the result, it only remains to convert hexadecimal value in decimal value to obtain 3211296...

Best regards,

Samuel

Read only

0 Likes
932

Thanks Samuel, that's a helpful answer, but I guess the problem that I have is why doesn't SAP do this conversion for me? After all, I can MOVE '1' TO integer_field and get 1 in integer_field.

I have found this statement in SAP help, but I don't understand the restrictions:

'Provided the field symbol is either fully typed or has one of the generic built-in ABAP types u2013 c, n, p, or x u2013 you can use the following ASSIGN statement...'

Read only

0 Likes
932

Yes I understand what you mean... But ASSIGN is not the right tool to make conversions : you have MOVE or WRITE in order to do conversions...

And as indicated in SAP help, ASSIGN work on memory area and not on objects... There is no more types except the one that you give to this command in CASTING TYPE...

Best regards,

Samuel

Read only

0 Likes
932

Hi Ged,

SAP online help says:

"If you use addition CASTING in casting_spec, the memory area is treated as if it had the type specified by CASTING."

For Move, it says "..convert the content of source into the data type of destination according to one of the conversion rules. "

This means if you want an integer result, you have to MOVE, i.e. in your case create a dynamic data object:

REPORT ZTEST_CASTING.
  TYPES:
    mytype2 type i.
  DATA:
    smallfield  type c LENGTH 5,
    smallfield2 type c LENGTH 4 VALUE '1',
    lr_data     type ref to data
  FIELD-SYMBOLS: 
    <fa>        type ANY.
CREATE DATA lr_data TYPE mytype2.
ASSIGN lr_data->* TO <any>.
<any> = smallfield2
*ASSIGN smallfield2 to <fa> CASTING type mytype2.
write: / smallfield2, <fa>.

Regards,

Clemens

Read only

ged_hurst
Participant
0 Likes
932

Clemens and Samuel, thanks for your help - between you, you have answered my question and solved my problem.