Conversion ABAP between types is a main topics in an ABAP developer.
Actually, SAP stores sometimes the same information/Data in different table or the module function requests a different types for the input or output.
This article will cover Conversion in ABAP Coding between the most used types : Binary, String and Table.
Will handling with data or attachment, few conversion function will be helpful in order to extract, save file.
Up to my experience, the most used conversions in ABAP will be:
Table of Contents
Welcome to most useful conversion for ABAP Developer: How to convert STRING to INTEGER in ABAP
First, the String is types STRING in ABAP and Integer is a just an ‘I‘ in ABAP Typing.
The simplest way is to use the standard function CONVERT_STRING_TO_INTEGER in any ABAP Code.
The CONVERT_STRING_TO_INTEGER also controls if the string is only a number or not :
The ABAP Code Snippet to user directly in your custom ABAP conversion program is :
DATA: lv_int TYPE i. DATA: lv_string TYPE string VALUE '12345'. "-----------------------------------------" " Convert String to Integer in ABAP "-----------------------------------------" CALL FUNCTION 'CONVERT_STRING_TO_INTEGER' EXPORTING p_string = lv_string IMPORTING p_int = lv_int EXCEPTIONS overflow = 1 invalid_chars = 2 OTHERS = 3. IF sy-subrc <> 0. MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4. ENDIF.
ABAP Decimal Type is more complicated than ABAP Integer because of the Decimal separator.
So, in order to convert String to Decimal in ABAP, you have to set with Decimal Separator is used.
In ABAP Workbench, you can use the standard function HRCM_STRING_TO_AMOUNT_CONVERT to made the Conversion ABAP.
ABAP Convert from Internal format into external format ( or vice versa ) can be done by CONVERSION_EXIT_ALPHA* function.
CONVERSION_EXIT_ALPHA_INPUT : convert external to internal
CONVERSION_EXIT_ALPHA_OUTPUT : convert internal to external
These functions are very useful to remove remove leading zeros in ABAP for example.
See also Different Methods to Convert String to Integer, Quantity, Currency in SAP ABAP
In Order to convert BINARY to XSTRING, first get the BINARY data and get the length of the binary ABAP variable.
Then Call the standard function SCMS_BINARY_TO_XSTRING and pass the length and the binary value. And Hop ! you get the XSTRING.
It can be useful when dealing/ conversion ABAP with Attachments or images (mime-type) that are stored as Binary in SAP tables
"--------------------------------------------" " Convert Binary to Xstring "--------------------------------------------" CALL FUNCTION 'SCMS_BINARY_TO_XSTRING' EXPORTING input_length = lv_length IMPORTING buffer = lv_buffer TABLES binary_tab = et_data EXCEPTIONS failed = 1 OTHERS = 2. IF sy-subrc <> 0. RAISE error_conversion. ENDIF
As you know, SAP has a very particular way to handle long text.
Usually, the SAP text is converted into lines.
Use the Standard Function CONVERT_TABLE_TO_STRING to collect all the text in one STRING variable in ABAP.
"--------------------------------------------" " Convert Text lines into string "--------------------------------------------" CALL FUNCTION 'CONVERT_TABLE_TO_STRING' EXPORTING i_tabline_length = lv_nb_lines IMPORTING e_string = lv_body_string TABLES it_table = lt_lines.
How to Convert String to Xstring in ABAP ( from String type to a binary format )
Just use the standard function SCMS_STRING_TO_XSTRING.
Read more about Data Type.
For the input
"--------------------------------------------" " Function to Convert String to XString "--------------------------------------------" CALL FUNCTION 'SCMS_STRING_TO_XSTRING' EXPORTING text = lv_body_string * mimetype = '' encoding = lv_encoding IMPORTING buffer = lv_body_xstring EXCEPTIONS failed = 1 OTHERS = 2. IF sy-subrc <> 0. * Implement suitable error handling here ENDIF.
Once you get the binary, you can convert the Binary string the XSTRING to table of XSTRING with this function SCMS_STRING_TO_BINARY to do conversion abap.
"--------------------------------------------" " Convert XString to SOLIX_TAB "--------------------------------------------" CALL FUNCTION 'SCMS_XSTRING_TO_BINARY' EXPORTING buffer = lv_body_xstring TABLES binary_tab = et_body_hex.
Actually, these conversion ABAP functions will be helpful for retrieving business object file and add it as attachment to an email.
SAP ISU Tables for Technical master data, for Billing Master Data, Billing and invoicing Tables?…
SAP Fiori 3 UX and Design of SAP Fiori Apps for SAP S/4HANA, SAP TechEd…
Dunning is the business practice of informing a customer of past due payment. Dunning letters…
SAP AP Tcodes & SAP AR Tcodes: House Banks Tcodes, Advance Payments or Down Payments,…
What are the main SAP Payment Terms Tables ? What are the related Tables in…
Work center consists of master data related to routing of products. It contains data related…