Conversion ABAP: between Binary, String, XString and Table

SAP ABAP

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:

  • Abap convert string to integer
  • Abap convert String to Decimal
  • Conversion from internal to external format
  • Convert Binary to XString: (attachment , … ).
  • Text lines into String conversion for ( long text , … )
  • Convert String to XString
  • XString to SOLIX_TAB for attachment, file …

Conversion ABAP: string to Integer

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 :

  • sy-subrc = 1 : if the string’s integer length overload the length of ABAP integer.
  • sy-subrc = 2 : if the string doesn’t contain only number

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 convert String to Decimal

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.

Basic ABAP Conversion from internal to external format

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

Conversion ABAP Binary to XSTRING

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

Convert Text lines into string

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.

ABAP Convert String to XString

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

  • BUFFER is typed XSTRING
  • also mimetype : the type of the binary ( jpg … exe. .. )
  • encoding:the encoding of the output string ( it is useful when you deal with non-unicode character)
   "--------------------------------------------"
   " 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.

Convert XString to SOLIX_TAB

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.