Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Wrong Encoding for TXT-Attachement with BCS Class

0 Likes
3,032

Hello,

I'm trying to send a TXT-File via mail.
I have an internal table with different fields (not only strings).
The TXT file in the attachement has the Codepage 4110 UTF-8 but I need it to be 1100 iso-8859-1.

I have tried a lot of different conversions but nothing seems to work.
My coding looks as follows:

      LOOP AT itab INTO s_itab.
        DO.
          UNASSIGN <fs>.
          ASSIGN COMPONENT sy-index OF STRUCTURE s_itab TO <fs>.
          IF <fs> IS ASSIGNED.
            CONCATENATE lv_string <fs> INTO lv_string.
          ELSE.
            EXIT.
          ENDIF.
        ENDDO.
        CONCATENATE lv_data_string lv_string cl_abap_char_utilities=>cr_lf INTO lv_data_string.
        CLEAR: s_itab, lv_string.
      ENDLOOP.
 #############################

    gv_soli = cl_bcs_convert=>string_to_soli( iv_string = lv_data_string ).


      CALL METHOD gr_document->add_attachment
        EXPORTING
          i_attachment_type     = 'TXT'  
          i_attachment_subject  = 'Test'
*         i_attachment_size     =     
*          i_attachment_language =     " 
          i_att_content_text    = gv_soli   "
*         i_att_content_hex     =   )
*         i_attachment_header   = 
*         iv_vsi_profile        =     " Viren-Scan-Profil
        .

1 ACCEPTED SOLUTION
Read only

Sandra_Rossi
Active Contributor
2,815

When you do the encoding with code page 1100 for ISO-8859-1 (API by order of preference based on your release: CL_ABAP_CONV_CODEPAGE (>=7.53), CL_ABAP_CODEPAGE (>=7.02), CL_ABAP_CONV_OUT_CE), you convert your text into bytes, then you send the bytes via the parameter I_ATT_CONTENT_HEX.

Hello,

I'm trying to send a TXT-File via mail.
I have an internal table with different fields (not only strings).
The TXT file in the attachement has the Codepage 4110 UTF-8 but I need it to be 1100 iso-8859-1.

I have tried a lot of different conversions but nothing seems to work.
My coding looks as follows:

      LOOP AT itab INTO s_itab.
        DO.
          UNASSIGN <fs>.
          ASSIGN COMPONENT sy-index OF STRUCTURE s_itab TO <fs>.
          IF <fs> IS ASSIGNED.
            CONCATENATE lv_string <fs> INTO lv_string.
          ELSE.
            EXIT.
          ENDIF.
        ENDDO.
        CONCATENATE lv_data_string lv_string cl_abap_char_utilities=>cr_lf INTO lv_data_string.
        CLEAR: s_itab, lv_string.
      ENDLOOP.
 #############################

    gv_soli = cl_bcs_convert=>string_to_soli( iv_string = lv_data_string ).


      CALL METHOD gr_document->add_attachment
        EXPORTING
          i_attachment_type     = 'TXT'  
          i_attachment_subject  = 'Test'
*         i_attachment_size     =     
*          i_attachment_language =     " 
          i_att_content_text    = gv_soli   "
*         i_att_content_hex     =   )
*         i_attachment_header   = 
*         iv_vsi_profile        =     " Viren-Scan-Profil
        .

7 REPLIES 7
Read only

Sandra_Rossi
Active Contributor
2,816

When you do the encoding with code page 1100 for ISO-8859-1 (API by order of preference based on your release: CL_ABAP_CONV_CODEPAGE (>=7.53), CL_ABAP_CODEPAGE (>=7.02), CL_ABAP_CONV_OUT_CE), you convert your text into bytes, then you send the bytes via the parameter I_ATT_CONTENT_HEX.

Read only

0 Likes
2,815

EDIT: Maybe I could add more:
I need it to be ANSI since UTF-8 without BOM can't be read correctly by my other software.

--

Thank you for your answer. I'm trying to figure out sending mails via SAP on my own, as well as the codepage stuff.
So please excuse me if I'm missing obvious parts.

I tried to implement it. I have to convert the string to a solix tab right after, correct?
Well my TXT Attachement now only contains bytes as you've said.

Is there a way to display them as before? Or does the codepage 1100 always display them as Bytes? How could a program like word read it?

cl_abap_codepage=>convert_to(
  EXPORTING
    source                        = lv_data_string    " source string
    codepage                      = `ISO-8859-1`    " codepage name
*    endian                        =     " Nicht benutzen (da wirkungslos!)
*    replacement                   = '#'    " Ersatzzeichen für Zeichensatzkonvertierung
*    ignore_cerr                   = ABAP_FALSE    " Ignoriere Konvertierungsfehler
  RECEIVING
    result                        = lv_data_string2
).

cl_bcs_convert=>string_to_solix(
  EXPORTING
    iv_string   =  lv_Data_string2   " Eingabedaten
*    iv_codepage =     " Zielcodepage in SAP Form  (Default = SAPconnect Einstellung)
*    iv_add_bom  =     " Byte-Order-Mark hinzufügen
  IMPORTING
    et_solix    =  gv_solix   " Ausgabedaten
*    ev_size     =     " Größe des Dokumentinhalts
).
<br>      CALL METHOD gr_document->add_attachment
        EXPORTING
          i_attachment_type     = 'TXT'  " oder XLS
          i_attachment_subject  = 'Test'
         i_att_content_hex     = gv_solix    " Inhalt (binär)

        .
<br>
Read only

0 Likes
2,815
Thimo Meiners your code is correct. Of course the software which displays the attachment has to recognize it's ISO-8859-1, but nothing says it in the file. With the Unicode BOM (UTF), softwares should be able to recognize automatically, if they are written well. If I open it with MS Word, there's a dialog to confirm the code page or select another one.When you say "codepage displays as bytes", I don't understand what you mean.
Read only

0 Likes
2,815

I'll provide an example:

I have both variables in the coding above.

Lv_data_string is = Dö;John;F;5648132;2000;[email protected];28.09.1983;;;;;;;\r\n

The txt file would contain above text but with the wrong encoding.

If I use the convert from above
Lv_data_string2 is = 406B25FC7A3B416C65763B463B31303038333438303B3524303054B412E414B5955455A40524F5A49414C413D545E4554253454E2E44453B32382E30392E313939333B3B3B3B3B3B3B0D0A

And the txt file contains that which is unreadable.

Read only

2,815

Thimo Meiners wow the hex code is not ISO-8859-1 at all. I just realized, did you define lv_data_string2 as XSTRING? You should also use XSTRING_TO_SOLIX.

Read only

2,815

Thank you Sandra Rossi, it worked 🙂

I defined lv_data_string2 as XSTRING and used xstring_to_solix.
Do you have a short explanation as for why I needed to use Xstring?
I thought using string_to_solix would work just as fine.

Read only

0 Likes
2,815

Thimo Meiners First I don't have any idea why string gave you this weird result. Now cl_abap_codepage=>convert_to returns a XSTRING, so use a XSTRING. Instead, if you store the result in a string, the string will be the XSTRING converted into the hexadecimal textual representation of the xstring, which is completely different.