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

Convert Xstring to Binary and Text



 
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: Sun Oct 14, 2007 9:35 pm    Post subject: Convert Xstring to Binary and Text Reply with quote

Requirement:
This is a sample code to show the conversion of a text internal table --> Xstring internal table --> Binary internal table --> Text internal table.

Processing:
It uses Function Modules 'SCMS_TEXT_TO_XSTRING', 'SCMS_XSTRING_TO_BINARY','SCMS_BINARY_TO_TEXT' for this purpose. Further, it downloads the itab in a text file on presentation server using Function Module GUI_DOWNLOAD.

Code:

REPORT  Z_RMTIWARI_XSTRING_TEXT_CONVER .
*----------------------------------------------------------------------*
*       Convert Xstring to Binary / Text - Date - 20.06.2005
*----------------------------------------------------------------------*
*       Written By: Ram Manohar Tiwari             
*----------------------------------------------------------------------*
*       Presented By: http://www.rmtiwari.com
*----------------------------------------------------------------------*

TYPES : BEGIN OF TY_TEXT,
          TEXT_FIELD(1000) TYPE C,
        END OF TY_TEXT.

TYPES : BEGIN OF TY_BINARY,
          BINARY_FIELD(1000) TYPE C,
        END OF TY_BINARY.

DATA : LV_XSTRING type XSTRING.
DATA : LT_BINARY type table of TY_BINARY with header line.
DATA : LT_TEXT type table of TY_TEXT with header line.
DATA : LT_TEXT_OUT type table of TY_TEXT with header line.

DATA : X_OUTFILE type string.

* Populate a text itab.
LT_TEXT-text_field = 'Hello'.
append LT_TEXT.

LT_TEXT-text_field = 'Hi'.
append LT_TEXT.

LT_TEXT-text_field = 'How r u?'.
append LT_TEXT.

* Convert text to xstring.
CALL FUNCTION 'SCMS_TEXT_TO_XSTRING'
* EXPORTING
*   FIRST_LINE       = 0
*   LAST_LINE        = 0
*   MIMETYPE         = ' '
 IMPORTING
   BUFFER           =  LV_XSTRING
  TABLES
    TEXT_TAB         = LT_TEXT
 EXCEPTIONS
   FAILED           = 1
   OTHERS           = 2
          .
IF SY-SUBRC <> 0.
  MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
          WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.

* Convert xstring to binary.
CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
  EXPORTING
    BUFFER                = LV_XSTRING
*   APPEND_TO_TABLE       = ' '
* IMPORTING
*   OUTPUT_LENGTH         =
  TABLES
    BINARY_TAB            = LT_BINARY
          .
* Convert binary to text.
CALL FUNCTION 'SCMS_BINARY_TO_TEXT'
  EXPORTING
    INPUT_LENGTH          = 100
*             FIRST_LINE            = 0
*             LAST_LINE             = 0
*             APPEND_TO_TABLE       = ' '
*             MIMETYPE              = ' '
    WRAP_LINES            = 'X'
*           IMPORTING
*             OUTPUT_LENGTH         =
  TABLES
    BINARY_TAB            = LT_BINARY
    TEXT_TAB              = LT_TEXT_OUT
*           EXCEPTIONS
*             FAILED                = 1
*             OTHERS                = 2
          .
IF SY-SUBRC <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.


X_OUTFILE = 'C:\stringtest.txt'.

* Download file.
CALL FUNCTION 'GUI_DOWNLOAD'
  EXPORTING
    FILENAME                = X_OUTFILE
    FILETYPE                = 'ASC'
  TABLES
    DATA_TAB                = LT_TEXT_OUT
  EXCEPTIONS
    FILE_WRITE_ERROR        = 1
    NO_BATCH                = 2
    GUI_REFUSE_FILETRANSFER = 3
    INVALID_TYPE            = 4
    NO_AUTHORITY            = 5
    UNKNOWN_ERROR           = 6
    HEADER_NOT_ALLOWED      = 7
    SEPARATOR_NOT_ALLOWED   = 8
    FILESIZE_NOT_ALLOWED    = 9
    HEADER_TOO_LONG         = 10
    DP_ERROR_CREATE         = 11
    DP_ERROR_SEND           = 12
    DP_ERROR_WRITE          = 13
    UNKNOWN_DP_ERROR        = 14
    ACCESS_DENIED           = 15
    DP_OUT_OF_MEMORY        = 16
    DISK_FULL               = 17
    DP_TIMEOUT              = 18
    FILE_NOT_FOUND          = 19
    DATAPROVIDER_EXCEPTION  = 20
    CONTROL_FLUSH_ERROR     = 21
    OTHERS                  = 22.
* Status of download
CASE SY-subrc.
  WHEN 0.
    MESSAGE I002(AQ) WITH
    'File downloaded as
 ' X_OUTFILE.

  WHEN OTHERS.
*        Upload unsuccessful - error message
    MESSAGE ID SY-MSGID TYPE 'E' NUMBER SY-MSGNO
       WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDCASE.

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.