Type ConversionsType Conversions Every time you assign a data object to a variable, the data types involved must either be compatible [Page 134] , that is, their technical attributes dat
Trang 2Functions for all Numeric Data Types
The following built-in functions work with all three numeric data types (F, I, and P) as arguments
Functions for all numeric data types
Function Result
ABS Absolute value of argument
SIGN Sign of argument: 1 X > 0
SIGN( X) = 0 if X = 0
-1 X < 0
CEIL Smallest integer value not smaller than the argument
FLOOR Largest integer value not larger than the argument
TRUNC Integer part of argument
FRAC Fraction part of argument
The argument of these functions does not have to be a numeric data type If you choose anothertype, it is converted to a numeric type For performance reasons, however, you should use thecorrect type whenever possible The functions itself do not have a data type of their own They donot change the numerical precision of a numerical operation
DATA N TYPE P DECIMALS 2
DATA M TYPE P DECIMALS 2 VALUE '-5.55'
N = ABS( M ) WRITE: 'ABS: ', N
N = SIGN( M ) WRITE: / 'SIGN: ', N
N = CEIL( M ) WRITE: / 'CEIL: ', N
N = FLOOR( M ) WRITE: / 'FLOOR:', N
N = TRUNC( M ) WRITE: / 'TRUNC:', N
N = FRAC( M ) WRITE: / 'FRAC: ', N
The output appears as follows:
Trang 3to type P Then the system processes the ABS function using the results of theconversion Then, during the assignment to the type C field T1, the result of thefunction is converted back to type C.
Floating-Point Functions
The following built-in functions work with floating point numbers (data type F) as an argument
Functions for floating point data types
ACOS, ASIN, ATAN; COS, SIN, TAN Trigonometric functions
COSH, SINH, TANH Hyperbolic functions
EXP Exponential function with base e (e=2.7182818285)
DATA: RESULT TYPE F,
Trang 4Business Calculations
Business Calculations
For calculations in business applications, use packed numbers The program attribute [Page 75]
Fixed point arithmetic affects calculations using packed numbers
If the program attribute Fixed point arithmetic is not set, type P fields are interpreted as integers
without decimal places The decimal places that you specify in the DECIMALS addition of theTYPES or DATA statement only affect how the field is formatted in the WRITE statement
DATA: PACK TYPE P DECIMALS 2.
decimal point) You should therefore always set the Fixed point arithmetic attribute when you use
type P fields
DATA: PACK TYPE P
PACK = 1 / 3 * 3
WRITE PACK
If you have not set the Fixed point arithmetic attribute, the result is 0, since the
calculation is performed using integer accuracy, and the result is therefore roundedinternally to 0
If the program attribute Fixed point arithmetic is set, the result is 1 because the result
of the division is stored internally as 0.333333333333333333333333333333 with anaccuracy of up to 31 digits
Trang 5Date and Time Calculations
Date and Time Calculations
Date and time fields have character types, not numeric ones However, you can still use dateand time fields in numeric operations To allow you to do so, the system performs automatic typeconversions [Page 187] You may also find it useful to use offset and length [Page 197]
specifications when using date and time fields in calculations
Example of a date calculation:
DATA: ULTIMO TYPE D
ULTIMO = SY-DATUM
ULTIMO+6(2) = '01' " = first day of this month
ULTIMO = ULTIMO - 1 " = last day of last month
Here, the last day of the previous month is assigned to the date field ULTIMO
1 ULTIMO is filled with the present date
2 Using an offset specification, the day is changed to the first day of the current month
3 1 is subtracted from ULTIMO Its contents are changed to the last day of the
previous month Before performing the subtraction, the system converts ULTIMO tothe number of days since 01.01.0001 and converts the result back to a date
Example of a time calculation:
DATA: DIFF TYPE I,
The number of hours between 02:00:00 and 20:00:00 is calculated
1 First, the difference between the time fields is calculated This is -64800, since T1and T2 are converted internally into 72000 and 7200 respectively
2 Second, with the operation MOD, this negative difference is converted to the totalnumber of seconds A positive difference would be unaffected by this calculation
3 Third, the number of hours is calculated by dividing the number of seconds by 3600
Trang 6Date and Time Calculations
CONVERT DATE <d1> INTO INVERTED-DATE <d2>
Afterwards, you can convert the inverted data back into a normal date using the statementCONVERT INVERTED-DATE <d1> INTO DATE <d2>
These statements convert the field <d1> from the formats DATE or INVERTED-DATE to theformats INVERTED-DATE or DATE and assign it to the field <d2>
For the conversion, ABAP forms the nine's complement
DATA: ODATE TYPE D VALUE '19955011',
IDATE LIKE ODATE
DATA FIELD(8)
FIELD = ODATE WRITE / FIELD
CONVERT DATE ODATE INTO INVERTED-DATE IDATE
FIELD = IDATE WRITE / FIELD
CONVERT INVERTED-DATE IDATE INTO DATE ODATE
FIELD = ODATE WRITE / FIELD
Output:
80049488
19955011
19955011
Trang 7Processing Character Strings
Processing Character Strings
ABAP contains a series of statements for processing character fields (types C, D, N, and T).There is no type conversion in these operations The statements treat all fields as though theywere type C fields, regardless of their actual types The internal form of type D, N, and T fields isthe same as for type C fields For this reason, you can use these statements for all character-type fields
Shifting Field Contents [Page 163]
Replacing Field Contents [Page 166]
Converting to Upper or Lower Case or Replacing Characters [Page 168]
Converting into a Sortable Format [Page 169]
Overlaying Strings [Page 170]
Finding Strings [Page 171]
Condensing Field Contents [Page 175]
Finding out the Length of a Character String [Page 174]
Concatenating Strings [Page 176]
Splitting Strings [Page 177]
There is also a variant of the MOVE statement that only works with strings:
Assigning Parts of Character Strings [Page 178]
Trang 8Shifting Field Contents
Shifting Field Contents
You can shift the contents of a field using the following variants of the SHIFT statement SHIFTmoves field contents character by character
Shifting a String by a Given Number of Positions
SHIFT <c> [BY <n> PLACES] [<mode>]
This statement shifts the field <c> by <n> positions If you omit BY <n> PLACES, <n> is
interpreted as one If <n> is 0 or negative, <c> remains unchanged If <n> exceeds the length of
<c>, <c> is filled out with blanks <n> can be variable
With the different <mode> options, you can shift the field <c> in the following ways:
• <mode> is LEFT:
Shifts the field contents <n> places to the left and adds <n> blanks at the right-hand end
of the field (default)
• <mode> is RIGHT:
Shift <n> positions to the right and adds <n> blanks at the left-hand end of the field
• <mode> is CIRCULAR:
Shift <n> positions to the left so that <n> characters on the left appear on the right
DATA: T(10) VALUE 'abcdefghij',
Trang 9Shifting Field Contents
Shifting a Structure up to a Given String
SHIFT <c> UP TO <str> <mode>
This statement searches the field contents of <c> until it finds the string <str> and shifts the field
<c> up to the edge of the field The <mode> options are the same as described above <str> can
Shifting a Structure According to the First or Last Character
SHIFT <c> LEFT DELETING LEADING <str>
SHIFT <c> RIGHT DELETING TRAILING <str>
This statement shifts the field <c> to the left or to the right, provided the first character on the left
Trang 10Shifting Field Contents
Trang 11Replacing Field Contents
Replacing Field Contents
To replace a string in a field with a different string, use the REPLACE statement
REPLACE <str1> WITH <str2> INTO <c> [LENGTH <l>]
The statement searches the field <c> for the first occurrence of the first <l> positions of thepattern <str1> If no length is specified, it searches for the pattern <str1> in its full length
Then, the statement replaces the first occurrence of the pattern <str1> in field <c> with the string
<str2> If a length <l> was specified, only the relevant part of the pattern is replaced
If the return code value of the system field SY-SUBRC is set to 0, this indicates that <str1> wasfound in <c> and replaced by <str2> A return code value other than 0 means that nothing wasreplaced <str1>, <str2>, and <len> can be variables
DATA: T(10) VALUE 'abcdefghij',
Trang 12Replacing Field Contents
Trang 13Converting to Upper or Lower Case or Replacing Characters
Converting to Upper or Lower Case or Replacing
Characters
The TRANSLATE statement converts characters into upper or lower case, or uses substitutionrules to convert all occurrences of one character to another character
Converting to Upper or Lower Case
TRANSLATE <c> TO UPPER CASE
TRANSLATE <c> TO LOWER CASE
These statements convert all lower case letters in the field <c> to upper case or vice versa
Substituting Characters
TRANSLATE <c> USING <r>
This statement replaces all characters in field <c> according to the substitution rule stored in field
<r> <r> contains pairs of letters, where the first letter of each pair is replaced by the secondletter <r> can be a variable
For more variants of the TRANSLATE statement with more complex substitution rules, see thekeyword documentation in the ABAP Editor
DATA: T(10) VALUE 'AbCdEfGhIj',
Trang 14Converting into a Sortable Format
Converting into a Sortable Format
The CONVERT TEXT statement converts strings into a format that can be sorted alphabetically.CONVERT TEXT <c> INTO SORTABLE CODE <sc>
This statement writes a string <c> to a sortable target field <sc> The field <c> must be of type Cand the field <sc> must be of type X with a minimum size of 16 times the size of <c>
The field <sc> can serve as an alphabetic sort key for <c> These sorts are applied to internaltables [Page 252] and extract datasets [Page 332] If you sort unconverted character fields, thesystem creates an order that corresponds to the platform-specific internal coding of the individualletters The conversion CONVERT TEXT creates target fields in such a way that, after sorting thetarget fields, the order of the corresponding character fields is alphabetical
The method of conversion depends on the text environment of the current ABAP program Thetext environment is fixed in the user master record, but can also be set from a program using thefollowing ABAP statement:
SET LOCALE LANGUAGE <lg> [COUNTRY <cy>] [MODIFIER <m>]
This statement sets the text environment according to the language <lg> With the option
COUNTRY, you can specify the country additionally to the language, provided there are specific differences for languages With the option MODIFIER, you can specify another identifier,provided there are differences in the language within one country For example, in Germany, theorder for sorting umlauts is different in a dictionary from the order used in a telephone book.The fields <lg>, <cy>, and <m> must be of type C and must have the same lengths as the keyfields of table TCP0C Table TCP0C is a table, in which the text environment is maintainedplatform-dependent During the statement SET LOCALE, the system sets the text environmentaccording to the entries in TCP0C With the exception of the platform identity, which is
country-transferred internally, the table key is specified with the SET statement The platform identifier ispassed implicitly If <lg> equals SPACE, the system sets the text environment according to theuser’s master record If there is no entry in the table for the key specified, the system reacts with
a runtime error
The text environment influences all operations in ABAP that depend on the character set
For more information about this topic, see the keyword documentation for CONVERT TEXT andSET LOCALE LANGUAGE
For an example of alphabetical sorting, see Sorting Internal Tables [Page 272]
Trang 15Overlaying Character Fields
Overlaying Character Fields
The OVERLAY statement overlays one string with another:
OVERLAY <c1> WITH <c2> [ONLY <str>]
This statement overlays all positions in field <c1> containing letters which occur in <str> with thecontents of <c2> <c2> remains unchanged If you omit ONLY <str>, all positions of <c1>
containing spaces are overwritten
If at least one character in <c1> was replaced, SUBRC is set to 0 In all other cases, SUBRC is set to 4 If <c1> is longer than <c2>, it is overlaid only in the length of <c2>
Trang 16Finding Character Strings
Finding Character Strings
To search a character field for a particular pattern, use the SEARCH statement as follows:SEARCH <c> FOR <str> <options>
The statement searches the field <c> for <str> starting at position <n1> If successful, the returncode value of SY-SUBRC is set to 0 and SY-FDPOS is set to the offset of the string in the field
<c> Otherwise, SY-SUBRC is set to 4
The search string <str> can have one of the following forms
<str> Function
<pattern> Searches for <pattern> (any sequence of characters) Trailing blanks are ignored <pattern> Searches for <pattern> Trailing blanks are not ignored
*<pattern> A word ending with <pattern> is sought
<pattern>* Searches for a word starting with <pattern>
Words are separated by blanks, commas, periods, semicolons, colons, question marks,
exclamation marks, parentheses, slashes, plus signs, and equal signs
<option> in the SEARCH FOR statement can be any of the following:
• ABBREVIATED
Searches the field <c> for a word containing the string in <str> The characters can beseparated by other characters The first letter of the word and the string <str> must bethe same
If the search string is found, all the characters in the search string (and all the characters
in between when using ABBREVIATED) are converted to upper case
DATA STRING(30) VALUE 'This is a little sentence.'
WRITE: / 'Searched', 'SY-SUBRC', 'SY-FDPOS'
ULINE /1(26)
SEARCH STRING FOR 'X'
WRITE: / 'X', SY-SUBRC UNDER 'SY-SUBRC',
SY-FDPOS UNDER 'SY-FDPOS'
SEARCH STRING FOR 'itt '
WRITE: / 'itt ', SY-SUBRC UNDER 'SY-SUBRC',
SY-FDPOS UNDER 'SY-FDPOS'
Trang 17Finding Character Strings
SEARCH STRING FOR '.e '
WRITE: / '.e ', SY-SUBRC UNDER 'SY-SUBRC',
SY-FDPOS UNDER 'SY-FDPOS'
SEARCH STRING FOR '*e'
WRITE: / '*e ', SY-SUBRC UNDER 'SY-SUBRC',
SY-FDPOS UNDER 'SY-FDPOS'
SEARCH STRING FOR 's*'
WRITE: / 's* ', SY-SUBRC UNDER 'SY-SUBRC',
SY-FDPOS UNDER 'SY-FDPOS'
SEARCH STRING FOR 'ft' ABBREVIATED
WRITE: / 'SY-FDPOS:', SY-FDPOS
POS = SY-FDPOS + 2
SEARCH STRING FOR 'ft' ABBREVIATED STARTING AT POS AND MARK
WRITE / STRING
WRITE: / 'SY-FDPOS:', SY-FDPOS
OFF = POS + SY-FDPOS -1
WRITE: / 'Off:', OFF
Trang 18Finding Character Strings
Trang 19Finding the Length of a Character String
Finding the Length of a Character String
The ABAP function STRLEN returns the length of a string up to the last character that is not aspace
[COMPUTE] <n> = STRLEN( <c> )
STRLEN processes any operand <c> as a character data type, regardless of its real type There
is no type conversion
As with mathematical functions [Page 157], the keyword COMPUTE is optional
DATA: INT TYPE I,
WORD1(20) VALUE '12345'
WORD2(20)
WORD3(20) VALUE ' 4 '
INT = STRLEN( WORD1 ) WRITE INT
INT = STRLEN( WORD2 ) WRITE / INT
INT = STRLEN( WORD3 ) WRITE / INT
The results are 5, 0, and 4 respectively
Trang 20Condensing Field Contents
Condensing Field Contents
The CONDENSE statement deletes redundant spaces from a string:
CONDENSE <c> [NO-GAPS]
This statement removes any leading blanks in the field <c> and replaces other sequences ofblanks by exactly one blank The result is a left-justified sequence of words, each separated byone blank If the addition NO-GAPS is specified, all blanks are removed
DATA: STRING(25) VALUE ' one two three four',
WRITE: / 'Length: ', LEN
CONDENSE STRING NO-GAPS
LEN = STRLEN( STRING )
Trang 21Concatenating Character Strings
Concatenating Character Strings
The CONCATENATE statement combines two or more separate strings into one
CONCATENATE <c1> <cn> INTO <c> [SEPARATED BY <s>]
This statement concatenates the character fields <c1> to <cn> and assigns the result to <c> Thesystem ignores spaces at the end of the individual source strings
The addition SEPARATED BY <s> allows you to specify a character field <s> which is placed inits defined length between the individual fields
If the result fits into <c>, SUBRC is set to 0 However, if the result has to be truncated, SUBRC is set to 4
SY-DATA: C1(10) VALUE 'Sum',
Sum - mer - holi - day
In C1 to C5, the trailing blanks are ignored The separator SEP retains them
Trang 22Splitting Character Strings
Splitting Character Strings
To split a character string into two or more smaller strings, use the SPLIT statement as follows:SPLIT <c> AT <del> INTO <c1> <cn>
The system searches the field <c> for the separator <del> The parts before and after the
separator are placed in the target fields <c1> <cn>
To place all fragments in different target fields, you must specify enough target fields Otherwise,the last target field is filled with the rest of the field <c> and still contains delimiters
If all target fields are long enough and no fragment has to be truncated, SY-SUBRC is set to 0.Otherwise it is set to 4
The output appears as follows:
Part 1 *** Part 2 *** Part 3 *** Part 4 *** Part 5
You can also split a string into the individual lines of an internal table as follows:
SPLIT <c> AT <del> INTO TABLE <itab>
The system adds a new line to the internal table <itab> for each part of the string
Trang 23Splitting Character Strings
Assigning Parts of Character Strings
The following variant of the MOVE statement works only with type C fields:
MOVE <c1> TO <c2> PERCENTAGE <p> [RIGHT]
Copies the percentage <p> percent of the character field <c1> left-justified (or right-justified ifspecified with the RIGHT option) to <c2>
The value of <p> can be a number between 0 and 100 The length to be copied from <f1> isrounded up or down to the next whole number
If one of the arguments in the statement is not type C, the parameter PERCENTAGE is ignored
DATA C1(10) VALUE 'ABCDEFGHIJ',
Trang 24Single Bit Processing in Hexadecimal Fields
Single Bit Processing in Hexadecimal Fields
In a hexadecimal field (type X), you can process the individual bits ABAP interprets the contents
of hex fields byte by byte A hexadecimal field with length n is n bytes long, and has a displaylength in ABAP of 2xn The decimal values 0 - 255 are represented in hexadecimal by the
Decimal value in byte sequence
Binary representation of one byte
Hexadecimal representation
The illustration shows how a byte in a sequence with the decimal value 181 is represented inhex The first four bits have the decimal value 11, the second four bits have the decimal value 5.This leads to the hexadecimal value ‘B5’ - 11x16+5 = 181
ABAP contains statements that allow you to read and set the individual bits in a type X field.There are also special logical operators that you can use to compare bit sequences [Page 234].Bit sequence processing allows you to process complex conditions and set operations moreefficiently
Setting and Reading Bits [Page 180]
Bit Operations [Page 182]
Set Operations Using Bit Sequences [Page 184]
Trang 25Setting and Reading Bits
Setting and Reading Bits
In a hexadecimal field (type X), you can set or read individual bits
Setting Bits
To set an individual bit, use the statement
SET BIT <n> OF <f> [TO <b>]
This statement sets the bit at position <n> of field <f> to 1 (or to the value of field <b>) Thesystem must be able to interpret field <n> as a positive integer The field <f> must have datatype X The field <b> must contain the value 0 or 1 If the bit is set, SY-SUBRC is set to 0 If <n>
is greater than the length of <f>, SY-SUBRC is unequal to zero If <n> or <b> contain invalidvalues, a runtime error occurs
DATA HEX(3) TYPE X.
SET BIT: 09 OF HEX TO 1,
The bits of the second byte in the three-character hexadecimal field HEX are set to
‘10110101’, and the list output is as follows:
00B500
The decimal value of the second byte is 181
Reading Bits
To read an individual bit, use the statement
GET BIT <n> OF <f> INTO <b>
This statement reads the bit at position <n> of field <f> into field <b> The system must be able
to interpret field <n> as a positive integer The field <f> must have data type X If the bit is read,SY-SUBRC is set to 0 If <n> is greater than the length of <f>, SY-SUBRC is unequal to zero and
<b> is set to zero If <n> contains an invalid value, a runtime error occurs
Trang 26Setting and Reading Bits
Here, the eight buts of the single-character hexadecimal field HEX (value ‘B5’) areread and displayed as follows:
10110101
Trang 27<bitexp> can be one of the following bit expressions:
Bit expression Meaning
BIT-NOT <y> Negation
<y> BIT-AND <z> And
<y> BIT-XOR <z> Exclusive or
<y> BIT-OR <z> Or
The operands <y> and <z> are linked bit by bit in the above operators, and the result is placed inthe result field <x> The result field <x>, and the operands <y> and <z> must all be of type X Ifthe field lengths are different, all operands are converted [Page 188] to the field length of theresult field <x>
The following rules apply to bit operations:
<y> <z> BIT-NOT <y> <y> BIT-AND <z> <y> BIT-XOR <z> <y> BIT-XOR <z>
As with mathematical expressions, you can use parentheses with bit expressions
DATA: HEX1(1) TYPE X VALUE 'B5',
HEX2(1) TYPE X VALUE '5B', HEX3(1) TYPE X.
HEX3 = BIT-NOT ( HEX1 BIT-XOR HEX2 ).
WRITE HEX3.
The output is:
Trang 28Bit Operations
1 0 1 1 0 1 0 1
B 5 HEX1
Hexadecimal
5 B HEX2
0 1 0 1 1 0 1 1
1 1 1 0 1 1 1 0 0 0 0 1 0 0 0 1 HEX1
Trang 29Set Operations Using Bit Sequences
Set Operations Using Bit Sequences
If you have a set of m elements, you can represent any subset as a sequence of bits If the nthelement of the set is present in a subset, the nth bit is set to 1, otherwise, it is set to 0 The
universal set therefore contains no zeros
In ABAP, you can represent a set of m elements using a field with type X and length of at leastm/8 To include a member of the universal set in a set, use the SET BIT statement To checkwhich members of the universal set are included in a particular set, use the GET BIT statement.You can use bit operations for the following set operations:
Set operation Bit operation
Intersection BIT-AND
Symmetrical difference BIT-XOR
You can also use the O operator when comparing bit sequences [Page 234] to determine
whether a particular set is a subset of another
DATA: FRANKFURT(4) TYPE X,
FRISCO(4) TYPE X, INTERSECT(4) TYPE X, UNION(4) TYPE X,
DATA: CARRID TYPE SPFLI-CARRID,
CARRIER LIKE SORTED TABLE OF CARRID
WITH UNIQUE KEY TABLE LINE.
DATA WA TYPE SPFLI.
SELECT CARRID FROM SCARR INTO TABLE CARRIER.
SELECT CARRID CITYFROM FROM SPFLI
INTO CORRESPONDING FIELDS OF WA.
WRITE: / WA-CARRID, WA-CITYFROM.
READ TABLE CARRIER FROM WA-CARRID TRANSPORTING NO FIELDS.
CASE WA-CITYFROM.
WHEN 'FRANKFURT'.
SET BIT SY-TABIX OF FRANKFURT.
WHEN 'SAN FRANCISCO'.
SET BIT SY-TABIX OF FRISCO.
Trang 30Set Operations Using Bit Sequences
WRITE 'Airlines flying from Frankfurt and San Francisco:'.
This produces the following output list:
The program uses four hexadecimal fields with length 4 - FRANKFURT, FRISCO,INTERSECT, and UNION Each of these fields can represent a set of up to 32elements The basic set is the set of all airlines from database table SCARR Eachbit of the corresponding bit sequences representes one airline To provide an index,the external index table CARRIER is created and filled with the airline codes from
Trang 31Set Operations Using Bit Sequences
table SCARR It is then possible to identify an airline using the internal index of tableCARRIER
In the SELECT loop for database table SPFLI, the corresponding bit for the airline isset either in the FRANKFURT field or the FRISCO field, depending on the departurecity The line number SY-TABIX is determined using a READ statement in which nofields are transported
The intersection and union of FRANKFURT and FRISCO are constructed using thebit operations BIT-AND and BIT-OR
The bits in INTERSECT and UNION are read one by one and evaluated in two DOloops For each position in the fields with the value 1, a READ statement retrievesthe airline code from the table CARRIER
Trang 32Type Conversions
Type Conversions
Every time you assign a data object to a variable, the data types involved must either be
compatible [Page 134] , that is, their technical attributes (data type, field length, number ofdecimal places) must be identical, or the data type of the source field must be convertible into thedata type of the target field
In ABAP, two non-compatible data types can be converted to each other if a correspondingconversion rule exists If data types are compatible, no conversion rule is necessary
If you use the MOVE statement to transfer values between non-compatible objects, the value ofthe source object is always converted into the data type of the target object With all ABAPoperations that perform value assignments between data objects (for example, arithmetic
operations or filling internal tables), the system handles all the necessary type conversions as forthe MOVE statement If you try to assign values between two data types for which no conversionrule exists, a syntax error or runtime error occurs
The following sections contain the conversion rules for incompatible ABAP data types:
Conversion Rules for Elementary Data Types [Page 188]
Conversion Rules for References [Page 192]
Conversion Rules for Structures [Page 193]
Conversion Rules for Internal Tables [Page 195]
With some ABAP statements that pass data between different objects, the alignment of the dataobjects is also important
Alignment of Data Objects [Page 196]
Trang 33Conversion Rules for Elementary Data Types
Conversion Rules for Elementary Data Types
There are eight predefined ABAP data types [Page 97] There are 64 possible type combinationsbetween these elementary data types ABAP supports automatic type conversion and lengthadjustment for all of them except type D (date) and type T (time) fields which cannot be
converted into each other
The following conversion tables define the rules for converting elementary data types for allpossible combinations of source and target fields
Source Type Character
Conversion table for source type C
Target Conversion
C The target field is filled from left to right If it is too long, it is filled with blanks from theright If it is too short, the contents are truncated at the right-hand end
D The character field should contain an 8-character date in the format YYYYMMDD
F The contents of the source field must be a valid representation of a type F field asdescribed in Literals [Page 120]
N Only the digits in the source field are copied The field is right-justified and filled withtrailing zeros
I
P The source field must contain the representation of a decimal number, that is, asequence of digits with an optional sign and no more than one decimal point Thesource field can contain blanks If the target field is too short, an overflow may occur.This may cause the system to terminate the program
T The character field should contain a 6-character time in HHMMSS format
X Since the character field should contain a hexadecimal-character string, the only validcharacters are 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F This character string is packed as ahexadecimal number, transported left-justified, and padded with zeros or truncated onthe right
Source Type Date
Conversion table for source type D
Target Conversion
C The date is transported left-justified without conversion
D Transport without conversion
F The date is converted into a packed number The packed number is then convertedinto a floating point number (see corresponding table)
N The date is converted into a character field The character field is then converted into
a numeric text field (see corresponding table)
Trang 34Conversion Rules for Elementary Data Types
Source Type Floating Point Number
Conversion table for source type F
Target Conversion
C The floating point number is converted to the <mantissa>E<exponent> format andtransported to the character field The value of the mantissa lies between 1 and 10unless the number is zero The exponent is always signed If the target field is tooshort, the mantissa is rounded The length of the character field should be at least 6bytes
D The source field is converted into a packed number The packed number is thenconverted into a date field (see corresponding table)
F Transport without conversion
N The source field is converted into a packed number The packed number is thenconverted into a numeric text field (see corresponding table)
I
P The floating point number is converted to an integer or fixed point value and, ifnecessary, rounded
T The source field is converted into a packed number The packed number is thenconverted into a time field (see corresponding table)
X The source field is converted into a packed number The packed number is thenconverted into a hexadecimal number (see corresponding table)
Source Type Integer
Type I is always treated in the same way as type P without decimal places Wherever type P ismentioned, the same applies to type I fields
Source Type Numeric Text
Conversion table for source type N
Target Conversion
C The numeric field is treated like a character field Leading zeros are retained
D The numeric field is converted into a character field The character field is thenconverted into a date field (see corresponding table)
F The numeric field is converted into a packed number The packed number is thenconverted into a floating point number (see corresponding table)
N The numeric field is transported right-justified and padded with zeros or truncated onthe left
Trang 35Conversion Rules for Elementary Data Types
Source Type Packed Number
If the program attribute Fixed point arithmetic is set, the system rounds type P fields according to
the number of decimal places or fills them out with zeros
Conversion table for source type P
Target Conversion
C The packed field is transported right-justified to the character field, if required with adecimal point The first position is reserved for the sign Leading zeros appear asblanks If the target field is too short, the sign is omitted for positive numbers If this isstill not sufficient, the field is truncated on the left ABAP indicates the truncation with
an asterisk (*) If you want the leading zeros to appear in the character field, useUNPACK instead of MOVE
D The packed field value represents the number of days since 01.01.0001 and isconverted to a date in YYYYMMDD format
F The packed field is accepted and transported as a floating point number
N The packed field is rounded if necessary, unpacked, and then transported justified The sign is omitted If required, the target field is padded with zeros on theleft
right-I
P The packed field is transported right-justified If the target field is too short, anoverflow occurs
T The packed field value represents the number of seconds since midnight and isconverted to a time in HHMMSS format
X The packed field is rounded if necessary and then converted to a hexadecimal
number Negative numbers are represented by the two's complement If the targetfield is too short, the number is truncated on the left
Source Type Time
Conversion table for source type T
Target Conversion
C The source field is transported left-justified without conversion
D Not supported Results in an error message during the syntax check or in a runtimeerror
F The source field is converted into a packed number The packed number is thenconverted into a floating point number (see corresponding table)
N The date is converted into a character field The character field is then converted into
a numeric text field (see corresponding table)
I
P The date is converted to the number of seconds since midnight.
T Transport without conversion
X The date is converted to the number of seconds since midnight in hexadecimal
Trang 36Conversion Rules for Elementary Data Types
Target Conversion
C The value in the hexadecimal field is converted to a hexadecimal character string,transported left-justified to the target field, and padded with zeros
D The source field value represents the number of days since 01.01.0001 and is
converted to a date in YYYYMMDD format
F The source field is converted into a packed number The packed number is thenconverted into a floating point number (see corresponding table)
N The source field is converted into a packed number The packed number is thenconverted into a numeric text field (see corresponding table)
I
P
The value of the source field is interpreted as a hexadecimal number It is converted
to a packed decimal number and transported right-justified to the target field If thehexadecimal field is longer than 4 bytes, only the last four bytes are converted If it istoo short, a runtime error may occur
T The source field value represents the number of seconds since midnight and isconverted to a time in HHMMSS format
X The value is transported left-justified and filled with X'00' on the right, if necessary
Trang 37Conversion Rules for References
Conversion Rules for References
ABAP currently uses class and interface variables within ABAP Objects [Page 1344] Both arepointers to objects You can assign values to them in the following combinations:
• If the two class references are incompatible, the class of the target field must be thepredefined empty class OBJECT
• When you assign a class reference to an interface reference, the class of the source fieldmust implement the interface of the target field
• If two interface references are incompatible, the interface of the target field must containthe interface of the source field as a component
• When you assign an interface reference to a class reference, the class of the source fieldmust be the predefined empty class OBJECT
Trang 38Conversion Rules for Structures
Conversion Rules for Structures
ABAP has one rule for converting structures that do not contain internal tables as components.There are no conversion rules for structures that contain internal tables You can only makeassignments between structures that are compatible
You can combine convertible structures in the following combinations:
• Converting a structure into a non-compatible structure
• Converting elementary fields into structures
• Converting structures into elementary fields
In each case, the system first converts all the structures concerned to type C fields and thenperforms the conversion between the two resulting elementary fields The length of the type Cfields is the sum of the lengths of the structure components This rule applies to all operationsusing structures that do not contain internal tables
If a structure is aligned [Page 196], the filler fields are also added to the length of the type C field
A non-aligned structure without filler fields:
C 75
If you convert a structure into a shorter structure, the original structure is truncated If you
convert a structure into a longer one, the parts at the end are not initialized according to theirtype, but filled with blanks
It can make sense to assign a structure to another, incompatible, structure if, for example, thetarget structure is shorter than the source, and both structures have the same construction overthe length of the shorter structure However, numeric components of structures that are filled inincompatible assignments may contain nonsensical or invalid values that may cause runtimeerrors
DATA: BEGIN OF FS1,
INT TYPE I VALUE 5,
PACK TYPE P DECIMALS 2 VALUE ‘2.26’,
TEXT(10) TYPE C VALUE ‘Fine text’,
FLOAT TYPE F VALUE ‘1.234e+05’,
DATA TYPE D VALUE ‘19950916’,
END OF FS1
DATA: BEGIN OF FS2,
INT TYPE I VALUE 3,
PACK TYPE P DECIMALS 2 VALUE ‘72.34’,
Trang 39Conversion Rules for Structures
TEXT(5) TYPE C VALUE ‘Hello’,
END OF FS2
WRITE: / FS1-INT, FS1-PACK; FS1-TEXT, FS1-FLOAT, FS1-DATE
WRITE: / FS2-INT, FS2-PACK, FS2-TEXT
Trang 40Conversion Rules for Internal Tables
Conversion Rules for Internal Tables
Internal tables can only be converted into other internal tables You cannot convert them intostructures or elementary fields
Internal tables are convertible if their line types are convertible The convertibility of internaltables does not depend on the number of lines
Conversion rules for internal tables:
• Internal tables which have internal tables as their line type are convertible if the internaltables which define the line types are convertible
• Internal tables which have line types that are structures with internal tables as
components are convertible according to the conversion rules for structures [Page 193] ifthe structures are compatible