‎2009 Oct 26 11:03 AM
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?
‎2009 Oct 26 11:21 AM
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>.
‎2009 Oct 26 11:21 AM
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>.
‎2009 Oct 26 12:42 PM
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?
‎2009 Oct 26 1:27 PM
Hi Ged,
You should have a look to hexadecimal values in debug :
SMALLFIELD2 -> 0031002000200020
<FA> -> 00310020and '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
‎2009 Oct 26 2:07 PM
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...'
‎2009 Oct 26 4:52 PM
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
‎2009 Oct 26 6:14 PM
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
‎2009 Nov 05 8:33 AM
Clemens and Samuel, thanks for your help - between you, you have answered my question and solved my problem.