Registration

Dear SAP Community Member,
In order to fully benefit from what the SAP Community has to offer, please register at:
http://scn.sap.com
Thank you,
The SAP Community team.
Skip to end of metadata
Go to start of metadata
REPORT z_file_hex_editor.

DATA go_textedit  TYPE REF TO cl_gui_textedit.
DATA gs_filetable TYPE LINE OF filetable.

PARAMETER: dummy(1).

AT SELECTION-SCREEN OUTPUT.
  PERFORM s1000_pbo.

AT SELECTION-SCREEN.
  PERFORM s1000_pai.

*&---------------------------------------------------------------------*
*&      Form  S1000_PBO
*&---------------------------------------------------------------------*
FORM s1000_pbo.
  IF go_textedit IS INITIAL.
    DATA lt_filetable    TYPE filetable.
    DATA l_rc           TYPE i.
    DATA l_action       TYPE i.
    CALL METHOD cl_gui_frontend_services=>file_open_dialog
      CHANGING
        file_table              = lt_filetable
        rc                      = l_rc
        user_action             = l_action
      EXCEPTIONS
        file_open_dialog_failed = 1
        cntl_error              = 2
        error_no_gui            = 3
        not_supported_by_gui    = 4
        OTHERS                  = 5.
    IF sy-subrc NE 0.
* error
      LEAVE PROGRAM.
    ELSEIF l_action NE cl_gui_frontend_services=>action_ok.
* Cancelled
      LEAVE PROGRAM.
    ELSE.
      READ TABLE lt_filetable INDEX 1 INTO gs_filetable.
      ASSERT sy-subrc = 0.
      DATA l_xstring TYPE xstring.
      DATA l_string TYPE string.
      DATA lt_textedit TYPE abaptxt255_tab.
      PERFORM read_bin_file USING gs_filetable-filename CHANGING l_xstring.
* Convert binary into hexa string (becomes string like "00FF...")
      l_string = l_xstring.
      CALL FUNCTION 'SWA_STRING_TO_TABLE'
        EXPORTING
          character_string = l_string
        IMPORTING
          character_table  = lt_textedit[].
      CREATE OBJECT go_textedit
        EXPORTING
          parent = cl_gui_container=>screen0.
      CALL METHOD go_textedit->set_wordwrap_behavior
        EXPORTING
          wordwrap_mode     = cl_gui_textedit=>wordwrap_at_fixed_position
          wordwrap_position = 16.
      CALL METHOD go_textedit->set_text_as_stream
        EXPORTING
          text = lt_textedit.
    ENDIF.
  ENDIF.
  SET TITLEBAR 'TIT' OF PROGRAM 'SAPLDSYP' WITH 'File Hex Viewer'(001).
ENDFORM.                                                    "s1000_pbo

*&---------------------------------------------------------------------*
*&      Form  S1000_PAI
*&---------------------------------------------------------------------*
FORM s1000_pai.
  TYPES char1 TYPE c LENGTH 1.
  DATA lt_textedit TYPE TABLE OF char1.
  DATA l_xstring TYPE xstring.
  DATA l_string TYPE string.
  CASE sy-ucomm.
    WHEN 'SPOS'.
* lt_textedit is made of 1 character lines, it's the only means to know
* the exact number of characters (so that dummy extra spaces are not added
* at the end of the file)
      CALL METHOD go_textedit->get_text_as_stream
        IMPORTING
          text = lt_textedit.
* Convert hexa string into binary (string like "00FF..." becomes bytes 00FF...)
      CALL FUNCTION 'SWA_STRING_FROM_TABLE'
        EXPORTING
          character_table  = lt_textedit[]
        IMPORTING
          character_string = l_string.
      l_xstring = l_string.
      PERFORM write_bin_file USING gs_filetable-filename l_xstring.
      MESSAGE e001(00) WITH 'File saved'(002) DISPLAY LIKE 'S'.
    WHEN OTHERS.
* keep from going to START-OF-SELECTION event (it would restart the
* program and global variables would be reset = DATA LOSS)
  MESSAGE e001(00) WITH 'Buttons deactivated'(001).
  ENDCASE.

ENDFORM.                                                    "s1000_pai

*&---------------------------------------------------------------------*
*&      Form  read_bin_file
*&---------------------------------------------------------------------*
FORM read_bin_file
      USING i_filename TYPE clike
      CHANGING e_file_xstring TYPE xstring.
  DATA l_filename TYPE string.
  DATA l_length TYPE i.
  DATA lt_x255 TYPE TABLE OF x255.
  l_filename = i_filename.
  CALL METHOD cl_gui_frontend_services=>gui_upload
    EXPORTING
      filename   = l_filename
      filetype   = 'BIN'
    IMPORTING
      filelength = l_length
    CHANGING
      data_tab   = lt_x255
    EXCEPTIONS
      OTHERS     = 1.
  IF sy-subrc = 0.
    CALL METHOD cl_swf_utl_convert_xstring=>table_to_xstring
      EXPORTING
        i_table  = lt_x255
        i_size   = l_length
      RECEIVING
        r_stream = e_file_xstring
      EXCEPTIONS
        OTHERS   = 3.
  ENDIF.
ENDFORM.                    "read_bin_file

*&---------------------------------------------------------------------*
*&      Form  write_bin_file
*&---------------------------------------------------------------------*
FORM write_bin_file
      USING
        i_filename      TYPE clike
        i_file_xstring  TYPE xstring.
  TYPES xx(50) TYPE x.
  DATA lt_xstring TYPE TABLE OF xx.
  DATA l_length TYPE i.
  DATA l_filename TYPE string.

  l_filename = i_filename.
  CALL METHOD cl_swf_utl_convert_xstring=>xstring_to_table
    EXPORTING
      i_stream = i_file_xstring
    IMPORTING
      e_table  = lt_xstring
    EXCEPTIONS
      OTHERS   = 3.
  l_length = XSTRLEN( i_file_xstring ).
  CALL METHOD cl_gui_frontend_services=>gui_download
    EXPORTING
      bin_filesize = l_length
      filename     = l_filename
      filetype     = 'BIN'
    CHANGING
      data_tab     = lt_xstring
    EXCEPTIONS
      OTHERS       = 3.
ENDFORM.                    "write_bin_file
  • No labels