SAP R/3 форум ABAP консультантов
Russian ABAP Developer's Club

Home - FAQ - Search - Memberlist - Usergroups - Profile - Log in to check your private messages - Register - Log in - English
Blogs - Weblogs News

Data Conversion from Char to binary



 
Post new topic   Reply to topic    Russian ABAP Developer's Club Forum Index -> Programming Techniques | Приемы программирования -> Conversion
View previous topic :: View next topic  
Author Message
admin
Администратор
Администратор



Joined: 01 Sep 2007
Posts: 1639

PostPosted: Wed Sep 05, 2007 4:42 pm    Post subject: Data Conversion from Char to binary Reply with quote

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       =
.

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
* 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.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Russian ABAP Developer's Club Forum Index -> Programming Techniques | Приемы программирования -> Conversion All times are GMT + 4 Hours
Page 1 of 1

 
Jump to:  
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.