Skip to Content
Author's profile photo Former Member

To remove spaces/null’s from Binary Data (of an internal table/variable) using ABAP

Hi all,
According to my current project requirement we needed to retrieve the specific Data of an attachment(mainly for .txt type of file) of PO:Purchase Order(which can only be retrieved in an

internal table in binary format) using function module ‘BBP_PD_PO_GETDETAIL’ and send it in base64 format using ‘SCMS_BASE64_ENCODE_STR’ Conversion method.
All were working fine but the data we were getting in the attachment was not as per standards.
we were getting a lots of “null”s at the end of our Data.(PFA-Attachment1 )
Large number of “null” characters were getting added at the end of the attachment contents when we opened the newly generated attachment as a text file from

target side/destination side.
The internal table in which we were getting this attachment data in binary format has the row contents (The only field ‘LINE’)as type of RAW and of length

1022.
Thus this length of 1022 was the issue because after assigning the attachment data it considers the remaining characters as “null” if the assigned data is not

large enough.
It can be undestood in more detail using following example.

e.g. :

The attachment that is the textfile of a Purchase Order contains the string data as :
Test Proxy completed//(The only Data needs to be sent as a contents of a text file )

Instead the textfile which was generated at destination contained data :

Test Proxy completed.nullnullnullnullnullnullnullnullnullnullnullnullnull
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
….(continued).”

(PFA-Attachment1 )

———————————————————————————————————
Actual Issue was:

      The actual issue was those nulls were getting added while retrieving this data in binary format itself(got it with the help of Debugging)
when we were calling that “BBP_PD_PO_GETDETAIL” method and retrieving the attachment data in Binary format in an internal table.
The size of normal string is much greater than the actual data provided hence it considers remaining spaces as “null”
(“0” in binary/hexadecimal format).

———————————————————————————————————-

So after few RND I came up with the solution for this issue which can be stated as follow:

Consider that we have retrieved binary Data into an internal table :
lt_contents.(Last field of lt_attachment table which is the outcome of ‘BBP_PD_PO_GETDETAI’ function module)
**********************************************************************************************************

DATA: lv_file TYPE string,
      lv_file_con1 TYPE xstring,
      lv_file_con2 TYPE xstring,
      lv_file_space type xstring,
      lv_file_con TYPE xstring.

LOOP AT lt_contents INTO lw_content.      // Get the RAW/Binary Data into a local work area for further usage.

lv_file_con1 = lw_content-line.
CONCATENATE  lv_file_con2 lv_file_con1 INTO lv_file_con2 IN BYTE MODE. //collecting the attachment contents in a variable from an internal table.

ENDLOOP.

lv_file_con = lv_file.
CLEAR lv_file.
lv_file_space = ‘0’.       // Hexadecimal symbol for space that is 0 which is to be eliminated.

shift lv_file_con2 right deleting trailing lv_file_space in byte mode.  // removing RHS spaces/’0’s/null’s.

**After RIGHT SHIFT  the spaces are get added at LHS of Data which can be eliminated using left shift.

shift lv_file_con2 left deleting leading lv_file_space in byte mode.  // all spacess/0’s/”null”s are get eliminated in this step

CALL FUNCTION ‘SCMS_BASE64_ENCODE_STR’      // converting Data into base64 format
  EXPORTING
  input    = lv_file_con2
**      unescape = ‘X’
  IMPORTING
  output   = lv_file.        // This lv_file will content the required data (without any “null” characters)hexadecimal form.

**————————————————————————————————————-

Thats it from my side.
If you expert guyz can suggest any changes I need to be made or can come up with a better solution
then you are most welcome.

Thanks and Regards,

Vikas Kailas Khanase
(+91) 9324758106

Assigned Tags

      13 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Former Member
      Former Member

      Thank you. It helpd us a lot.

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

      You are Welcome Gerbert

      Author's profile photo Former Member
      Former Member

      Thanks you very much. Saved very much time

      Author's profile photo madhumati mishra
      madhumati mishra

      Hi ,

      this solution doesnt work for me . I dont get correct string length after shifting and deleting. I am using xstrlength .

      below is the code -

      READ TABLE lt_content INTO ls_content_binary INDEX lv_count.

      *    lv_space = '0'.

      *    SHIFT ls_content_binary-line RIGHT DELETING TRAILING lv_space IN BYTE MODE.

      *    SHIFT ls_content_binary-line LEFT DELETING LEADING lv_space IN BYTE MODE.

           lv_len = xstrlen( ls_content_binary-line ).


      xstrlength still returns 1022. I see 0 in place for place in line even after shift.


      can you help.


      Thanks

      Madhumati

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

      hi Madhumati Mishra,

      If you are using xstrlen() make sure that you have used Conversion


      'SCMS_STRING_TO_XSTRING'


      I hope this will Help You.!

      Author's profile photo madhumati mishra
      madhumati mishra

      This function also didn't work for me . But anyway we had to shift for base64 string for some other requirement .

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

      I hope you have got what you wanted.. ➕

      Author's profile photo Nad Cuky
      Nad Cuky

      It didn't work for me too, but I found a solution (actually, a workaround) that depends on your specific use of the RAW1022 table after the conversion.

      If the RAW table is eventually sent to a function/method like CL_GUI_FRONTEND_SERVICES=>GUI_DOWNLOAD, then you can prevent it from saving the trailing spaces/zeros by sending the parameter BIN_FILESIZE which should simply contain the length of the whole xstring object. If you don't specify the file size then the method will save the complete RAW table including the spaces at the end.

      You can get the filesize before the conversion loop to the RAW table, by using XSTRLEN on the xstring itself.

      Instead of the conversion loop, you can use the function module 'SCMS_XSTRING_TO_BINARY' which returns a binary table (no specific length specified; you can use RAW1022, RAW255 or any length you want) and the aforementioned file size.

       

      Author's profile photo Ene Florin Catalin
      Ene Florin Catalin

      Hey, thanks for your comment! It helped a lot! 🙂

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

      Hello Cuky,

      This logic is used in order to send data directly to other system such as PI, Oracle and there we have to download the file.

      IF you have to download or upload respective contents in SAP directory or local machine then your logic is excellent.
      ?

       

      Author's profile photo Jimmy Moreno
      Jimmy Moreno

      Hi Vikas.

      I have my internal table lt_content with XML content, this is my code:

      DATA: lt_content TYPE soli.

      When my internal table is filled:

      And this is my output:

      Can you help me?

       

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

      Hi Jimmy,

      Can you specify the type of file you are using and how Data is getting populated  in lt_Content table?

      Thanks,

      Vikas Khansae

      +91 8806 133 203

      Author's profile photo Jimmy Moreno
      Jimmy Moreno

      Hi Vikas.

      I found the solution for my issue, I used another FM for insert the document:

      CALL FUNCTION 'SO_DOCUMENT_INSERT_API1'

      I sent my hexadecimal value and with this my output file didn't have null characters.

      Thanks you.

       

      Jimmy.