Author: Rashid Javed
A short discussion of conversions between character and binary data type in ABAP
In good old day of SAP when unicode was not there, the conversion from char data type to binary was very simple. Just a move statement would work.
Example (of good old days)
DATA: ch(1) TYPE c, BEGIN OF xt, xx(1) TYPE x, END OF xt. * now if you move, the xt-xx will show the asci number of that char. ch = 'A'. *Now xt-xx will have a value of 65 the asci code for A MOVE ch TO xt.
Similarly you can insert line feed or tab in your text using the followingError rendering macro 'code': Invalid value specified for parameter 'lang'
DATA: BEGIN OF xt, xx(1) TYPE x, END OF xt, cstr1(30) TYPE c. xt-xx = '09'. "Tab character CONCATENATE 'hello' xt 'world' INTO cstr1. "We have hello world separated by tab
Similarly for carriage return/ line feed you can assign values of '0D', '0A' to xt-xx and use it as a character in your program. However with the Unicode enable systems came the new rules in ABAP such as structures cannot be simply 'moved' to other and char to binary change was a little bit different.
Now first of all, for special character, SAP has provided a class CL_ABAP_CHAR_UTILITIES which has attributes such as HORIZONTAL_TAB, CR_LF, FORM_FEED that can be used in your string processing.
For example you read a file from application server that is tab separated. How would you identify the fields. Supposing that each row of file has just read into a string str1, following would be the solution
ExampleError rendering macro 'code': Invalid value specified for parameter 'lang'
DATA: itab TYPE STANDARD TABLE OF tdline, wa_tab TYPE tdline, str1 TYPE string. "we assume that tab separated value is already in this string SPLIT str1 AT cl_abap_char_utilities=>horizontal_tab INTO TABLE itab. IF sy-subrc = 0. *Now in itab you have separate fields which you can move to your internal table *...... *...... ENDIF.
Also note that the same class CL_ABAP_CHAR_UTILITIES can be used to insert these special characters into your character data. Again think of a situation where you want to save a tab delimited file on application server. Similarly it can be used to manually insert carriage returns line feeds etc.
Finally to convert char data to binary (possible uses can be the function module SO_NEW_DOCUMENT_ATT_SEND_API1 where you want to send an internal table or some data in binary format) we can use the following logic.
1: Get the system code page
2: create instance of cl_abap_conv_uc_number
3: use a method from this class called convert_char_stream to convert to binary.
DATA: codep TYPE cpnormcp, conu TYPE REF TO cl_abap_conv_uc_number. CALL FUNCTION 'SYSTEM_CODEPAGE' "get the current code page IMPORTING codepage = codep. CREATE OBJECT conu "instantiate cl_abap_conv_uc_number EXPORTING im_source_codepage = codep EXCEPTIONS converter_creation_failed = 1 OTHERS = 2. IF sy-subrc <> 0. WRITE:/ 'create object error CL_ABAP_CONV_UC_NUMBER'. EXIT. ENDIF. **Now lets say input data is in table asci_tab DATA: asci_list TYPE TABLE OF solisti1 WITH HEADER LINE, conhex TYPE solix OCCURS 1 WITH HEADER LINE, inp TYPE string, ous TYPE xstring, len TYPE i. LOOP AT asci_list. WRITE cl_abap_char_utilities=>cr_lf TO asci_list-line+253. "to put linefeed at end MOVE asci_list-line TO inp. "move char to input string CALL METHOD conu->convert_char_stream "call method to convert EXPORTING im_stream = inp IMPORTING ex_stream = ous ex_stream_len = len EXCEPTIONS conversion_error = 1 OTHERS = 2. IF sy-subrc <> 0. CONTINUE. "if error in conversion than do no append to table ELSE. MOVE ous TO conhex-line. "move the binary string to binary table APPEND conhex. ENDIF. ENDLOOP.
Now CONHEX can be used to attach as a binary object to the function module SO_NEW_DOCUMENT_ATT_SEND_API1.