Posted: Wed Sep 05, 2007 4:42 pm Post subject: Data Conversion from Char to binary
Author: Rashid Javed
Description
A short discussion of conversions between character and binary data type in ABAP
Details
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)
Code:
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 following
Code:
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
Example
Code:
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.
Example
Code:
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
* CURRENT_DYNAMIC_CODEPAGE =
.
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
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
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.
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You can download files in this forum
All product names are trademarks of their respective companies. SAPNET.RU websites are in no way affiliated with SAP AG. SAP, SAP R/3, R/3 software, mySAP, ABAP, BAPI, xApps, SAP NetWeaver and any other are registered trademarks of SAP AG. Every effort is made to ensure content integrity. Use information on this site at your own risk.