- SAP Smartforms
- Data Type Conversion
- Character Encoding and Data Transformation
- Conversion Functions
- Data Transformation for File Processing
- Time and Date Handling
- BAPI and IDOC Data Conversions
- Internal and External Storage Formats
- Conversion of Legacy Data
- ABAP Conversion Exit Functions
- Error Handling During Conversion
- Handling Different Formats for Integration
24CONVERSION2411 – Conversion Functions
1. CONV Operator (General Data Type Conversion)
The CONV operator is used for explicit conversion between compatible data types. It is a versatile tool that helps in transforming one data type to another, particularly useful when converting between strings, integers, floats, etc.
Syntax:
DATA(lv_target) = CONV target_type( source_value ).
Example:
Convert an integer to a string:
DATA : lv_int TYPE i VALUE 123,
lv_string TYPE string.
lv_string = CONV string( lv_int ). ” Converts integer to string
2. NUMC Function (Numeric Conversion)
The NUMC function converts a value into a numeric character field (type n), preserving leading zeros. This is useful when you need a number as a string of digits (e.g., formatting document numbers).
Syntax:
DATA(lv_numc) = CONV n(length)( source_value ).
Example:
DATA : lv_int TYPE i VALUE 45,
lv_numc TYPE n LENGTH 4.
lv_numc = CONV n(4)( lv_int ). ” Result: ‘0045’
3. STRING_TO_NUMBER Function
This function is used to convert a string (character-based field) into a numeric value. This is helpful when working with string inputs that represent numbers and need to be processed arithmetically.
Syntax:
DATA(lv_number) = STRING_TO_NUMBER( lv_string ).
Example:
DATA : lv_string TYPE string VALUE ‘123’,
lv_int TYPE i.
lv_int = STRING_TO_NUMBER( lv_string ). ” Converts string ‘123’ to integer 123
4. NUMBER_TO_STRING Function
This function converts a numeric value into a string. It’s useful when you want to display a number in a character-based format.
Syntax:
DATA(lv_string) = NUMBER_TO_STRING( lv_number ).
Example:
DATA: lv_int TYPE i VALUE 456,
lv_string TYPE string.
lv_string = NUMBER_TO_STRING( lv_int ). ” Converts integer 456 to string ‘456’
5. WRITE TO Statement (Data to String Conversion)
The WRITE TO statement is commonly used for converting various data types into a string format. This approach allows for formatted output, such as dates, numbers and floating-point values.
Syntax:
WRITE source_value TO target_string [FORMAT OPTIONS].
Example:
DATA : lv_date TYPE d VALUE ‘20241127’,
lv_string TYPE string.
WRITE lv_date TO lv_string. ” Converts date ‘20241127’ to string ‘27.11.2024’
6. XSYMBOL and XSYMBOL_TO_STRING (Hexadecimal Conversion)
These functions are used to convert data into hexadecimal representation and vice versa. This is useful for low-level data manipulation or when working with binary data.
Example:
- Convert a string to its hexadecimal value:
DATA : lv_string TYPE string VALUE ‘ABAP’,
lv_hex TYPE xstring.
lv_hex = cl_abap_conv_out_ce=>create( )->convert( lv_string ).
- Convert hexadecimal back to a string:
DATA : lv_hex TYPE xstring,
lv_string TYPE string.
lv_string = cl_abap_conv_in_ce=>create( )->convert( lv_hex ).
7. Date and Time Conversion Functions
ABAP provides several functions for converting dates and times between different formats.
- CONVERT DATE: Converts an internal date (YYYYMMDD) to an external format (human-readable).
- CONVERT TIME: Converts an internal time (HHMMSS) to an external format.
Example:
DATA : lv_date TYPE d VALUE ‘20241127’,
lv_text TYPE string.
CALL FUNCTION ‘CONVERT_DATE_TO_EXTERNAL’
EXPORTING
date_internal = lv_date
IMPORTING
date_external = lv_text.
WRITE : lv_text. ” Output: 27.11.2024 (depends on the user settings)
8. CHAR_TO_NUM and NUM_TO_CHAR Functions
These functions convert between character fields and numbers. They are useful when dealing with data in numeric formats stored as characters.
Example:
- Convert a character field to a number:
DATA : lv_char TYPE c LENGTH 5 VALUE ‘00123’,
lv_num TYPE i.
lv_num = CHAR_TO_NUM( lv_char ). ” Converts ‘00123’ to integer 123
- Convert a number to a character field:
DATA : lv_num TYPE i VALUE 45,
lv_char TYPE c LENGTH 5.
lv_char = NUM_TO_CHAR( lv_num ). ” Converts 45 to ‘00045’
9. CURRENCY_CONVERSION Function Module
This function module converts values between different currencies, considering exchange rates and decimal places for each currency.
Example:
Convert an amount from one currency to another:
DATA : lv_amount TYPE p DECIMALS 2 VALUE 1000,
lv_result TYPE p DECIMALS 2.
CALL FUNCTION ‘CURRENCY_CONVERSION’
EXPORTING
amount = lv_amount
currency_from = ‘USD’
currency_to = ‘EUR’
IMPORTING
converted_value = lv_result.
WRITE : lv_result.
10. FLTP_TO_CHAR and CHAR_TO_FLTP Functions
These functions handle conversions between floating-point numbers and character strings.
Example:
- Convert a float to a character string:
DATA : lv_float TYPE f VALUE ‘123.456’,
lv_string TYPE string.
lv_string = FLTP_TO_CHAR( lv_float ). ” Converts float to string ‘123.456’
- Convert a character string to a float:
DATA : lv_string TYPE string VALUE ‘789.123’,
lv_float TYPE f.
lv_float = CHAR_TO_FLTP( lv_string ). ” Converts string ‘789.123’ to float 789.123
These conversion functions in ABAP allow you to work with a variety of data types and ensure proper handling of data during operations, input/output or data transfers between systems.
Author : Aniket Pawar, 9373518385
24CONVERSION2411 – Conversion Functions
1. CONV Operator (General Data Type Conversion)
The CONV operator is used for explicit conversion between compatible data types. It is a versatile tool that helps in transforming one data type to another, particularly useful when converting between strings, integers, floats, etc.
Syntax:
DATA(lv_target) = CONV target_type( source_value ).
Example:
Convert an integer to a string:
DATA : lv_int TYPE i VALUE 123,
lv_string TYPE string.
lv_string = CONV string( lv_int ). ” Converts integer to string
2. NUMC Function (Numeric Conversion)
The NUMC function converts a value into a numeric character field (type n), preserving leading zeros. This is useful when you need a number as a string of digits (e.g., formatting document numbers).
Syntax:
DATA(lv_numc) = CONV n(length)( source_value ).
Example:
DATA : lv_int TYPE i VALUE 45,
lv_numc TYPE n LENGTH 4.
lv_numc = CONV n(4)( lv_int ). ” Result: ‘0045’
3. STRING_TO_NUMBER Function
This function is used to convert a string (character-based field) into a numeric value. This is helpful when working with string inputs that represent numbers and need to be processed arithmetically.
Syntax:
DATA(lv_number) = STRING_TO_NUMBER( lv_string ).
Example:
DATA : lv_string TYPE string VALUE ‘123’,
lv_int TYPE i.
lv_int = STRING_TO_NUMBER( lv_string ). ” Converts string ‘123’ to integer 123
4. NUMBER_TO_STRING Function
This function converts a numeric value into a string. It’s useful when you want to display a number in a character-based format.
Syntax:
DATA(lv_string) = NUMBER_TO_STRING( lv_number ).
Example:
DATA: lv_int TYPE i VALUE 456,
lv_string TYPE string.
lv_string = NUMBER_TO_STRING( lv_int ). ” Converts integer 456 to string ‘456’
5. WRITE TO Statement (Data to String Conversion)
The WRITE TO statement is commonly used for converting various data types into a string format. This approach allows for formatted output, such as dates, numbers and floating-point values.
Syntax:
WRITE source_value TO target_string [FORMAT OPTIONS].
Example:
DATA : lv_date TYPE d VALUE ‘20241127’,
lv_string TYPE string.
WRITE lv_date TO lv_string. ” Converts date ‘20241127’ to string ‘27.11.2024’
6. XSYMBOL and XSYMBOL_TO_STRING (Hexadecimal Conversion)
These functions are used to convert data into hexadecimal representation and vice versa. This is useful for low-level data manipulation or when working with binary data.
Example:
- Convert a string to its hexadecimal value:
DATA : lv_string TYPE string VALUE ‘ABAP’,
lv_hex TYPE xstring.
lv_hex = cl_abap_conv_out_ce=>create( )->convert( lv_string ).
- Convert hexadecimal back to a string:
DATA : lv_hex TYPE xstring,
lv_string TYPE string.
lv_string = cl_abap_conv_in_ce=>create( )->convert( lv_hex ).
7. Date and Time Conversion Functions
ABAP provides several functions for converting dates and times between different formats.
- CONVERT DATE: Converts an internal date (YYYYMMDD) to an external format (human-readable).
- CONVERT TIME: Converts an internal time (HHMMSS) to an external format.
Example:
DATA : lv_date TYPE d VALUE ‘20241127’,
lv_text TYPE string.
CALL FUNCTION ‘CONVERT_DATE_TO_EXTERNAL’
EXPORTING
date_internal = lv_date
IMPORTING
date_external = lv_text.
WRITE : lv_text. ” Output: 27.11.2024 (depends on the user settings)
8. CHAR_TO_NUM and NUM_TO_CHAR Functions
These functions convert between character fields and numbers. They are useful when dealing with data in numeric formats stored as characters.
Example:
- Convert a character field to a number:
DATA : lv_char TYPE c LENGTH 5 VALUE ‘00123’,
lv_num TYPE i.
lv_num = CHAR_TO_NUM( lv_char ). ” Converts ‘00123’ to integer 123
- Convert a number to a character field:
DATA : lv_num TYPE i VALUE 45,
lv_char TYPE c LENGTH 5.
lv_char = NUM_TO_CHAR( lv_num ). ” Converts 45 to ‘00045’
9. CURRENCY_CONVERSION Function Module
This function module converts values between different currencies, considering exchange rates and decimal places for each currency.
Example:
Convert an amount from one currency to another:
DATA : lv_amount TYPE p DECIMALS 2 VALUE 1000,
lv_result TYPE p DECIMALS 2.
CALL FUNCTION ‘CURRENCY_CONVERSION’
EXPORTING
amount = lv_amount
currency_from = ‘USD’
currency_to = ‘EUR’
IMPORTING
converted_value = lv_result.
WRITE : lv_result.
10. FLTP_TO_CHAR and CHAR_TO_FLTP Functions
These functions handle conversions between floating-point numbers and character strings.
Example:
- Convert a float to a character string:
DATA : lv_float TYPE f VALUE ‘123.456’,
lv_string TYPE string.
lv_string = FLTP_TO_CHAR( lv_float ). ” Converts float to string ‘123.456’
- Convert a character string to a float:
DATA : lv_string TYPE string VALUE ‘789.123’,
lv_float TYPE f.
lv_float = CHAR_TO_FLTP( lv_string ). ” Converts string ‘789.123’ to float 789.123
These conversion functions in ABAP allow you to work with a variety of data types and ensure proper handling of data during operations, input/output or data transfers between systems.
Author : Aniket Pawar, 9373518385