Skip to Content
Author's profile photo Former Member

How to properly convert SOLIX to XSTRING, without additional bytes

There are several use cases when you need to convert SOLIX table to XSTRING. For example, when you are using sap gateway for downloading files from SAP office, you will need to use conversion from solix to xstring.

You can use method cl_bcs_convert=>solix_to_xstring or FM  SCMS_BINARY_TO_XSTRING for this conversion.

Now, the issue: SOLIX is a table of RAW 255, that means every line of this table has 255 raw bytes. When you don’t specify output length of XSTRING variable, empty raw bytes from the last line are converted as well which means that you will supply corrupted file. It is be readable in most cases, but for example Microsoft Word will show warning message that the file is corrupted, though it can be opened afterwards.

Solution is to set number of bytes that needs to be converted (actual file size, not lines of table multiplied by size of line) as can be seen in following example:

    DATA:
      lt_file      TYPE solix_tab,
      lv_length   TYPE i,
      ls_doc_data TYPE sofolenti1.

    CALL FUNCTION 'SO_DOCUMENT_READ_API1'
      EXPORTING
        document_id                = iv_id
      IMPORTING
        document_data             = ls_doc_data
      TABLES
        contents_hex               = lt_file
      EXCEPTIONS
        document_id_not_exist      = 1
        operation_no_authorization = 2
        x_error                    = 3
        OTHERS                     = 4.
    IF sy-subrc = 0.
      lv_length = ls_doc_data-doc_size.

      IF lt_att IS NOT INITIAL.
        CALL FUNCTION 'SCMS_BINARY_TO_XSTRING'
          EXPORTING
            input_length = lv_length
          IMPORTING
            buffer       = ev_xstring_content
          TABLES
            binary_tab   = lt_file
          EXCEPTIONS
            failed       = 1
            OTHERS       = 2.
      ENDIF.
    ENDIF.

Assigned Tags

      8 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Custodio de Oliveira
      Custodio de Oliveira

      Hi Peter,

      This is the bit that makes your post stand out from the hundred other on this subject:

      actual file size, not lines of table multiplied by size of line


      ....


      lv_length = ls_doc_data-doc_size.

      It makes a huge difference. I feel bad for giving you only one star earlier 🙁

      May I suggest that you add a few more tags to this blog post so it will come first on search results?

      Regards,

      Custodio

      Author's profile photo Former Member
      Former Member
      Blog Post Author

      Hi Custodio,

      thank you for suggestion, I will try to improve it. Do not worry about the rating, just hope the article will find its audience when they need it 🙂

      Best regards,

      Peter

      Author's profile photo Former Member
      Former Member
      Blog Post Author

      BTW I added the first line you mentioned after twitter discussion, so you already have contributed to make this article more clear where is the improvement 🙂

      Author's profile photo Krishna Kishor Kammaje
      Krishna Kishor Kammaje

      Peter,

      I get this size as zero. What can be the issue?

      Thanks

      Krishna

      Author's profile photo Former Member
      Former Member
      Blog Post Author

      Hi Krishna,

      I am sorry, but I do not know what can be the problem.

      Regards,

      Peter

      Author's profile photo Heidi Juarez
      Heidi Juarez

      Thank you this was very helpful.

      Author's profile photo Prabaharan Asokan
      Prabaharan Asokan

      Thanks Peter. Helped me in resolving file corrupt error occurring in .docx, .pptx, .xlsx formats.

      Regards

      Prabha

      Author's profile photo Yoppie Ariesthio
      Yoppie Ariesthio

      Thank you!! It is working! my .xlsx file is not corrupting anymore.

      I tried with cl_bcs_convert=>solix_to_xstring( ) but it's giving me corrupts file.

      Once again, thanks!!!