Application Development 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: 

How to convert string to hexadecimal in ABAP

Former Member
0 Kudos

Hi Experts,

There was a special character which was causing error while generating the PDF using dynamic FM. Its an invalid special character which XML technology wont accept.

SAP has suggested to implement a NOTE in which we can check and handle all those invalid characters which was causing error while rendering the PDF.

If i am using that character directly in abap code[snapshot attached] it is causing me priority 1 ATC check, so i have to handled it in another way.

After searching google i found that the other way is

1. i need to first convert the string to hexadecimal code

2. Compare the special character hexadecimal value and remove it from full string hexadecimal code.

Now my question is, can you please help me how to convert the string to hexadecimal code and how i will find the hexadecimal code for special character mentioned in the snapshot.

Please help me on this.

Thanks,

Best Regards,

Bhupinder

1 ACCEPTED SOLUTION

GeorgeShlyakhov
Participant
0 Kudos

Hi Bhupinder!

Try to refer to the code below:


REPORT ztest.

DATA:

  l_text        TYPE string VALUE 'Some text ┐ some text',

  l_letter      TYPE string VALUE '┐',

  l_xspace      TYPE xstring,

  l_xletter     TYPE xstring,

  l_xstring     TYPE xstring,

  lv_bin_outlen TYPE i,

  gt_bin        TYPE STANDARD TABLE OF sdokcntbin.

WRITE: / 'Before:', l_text.

CALL FUNCTION 'SCMS_STRING_TO_XSTRING'

  EXPORTING

   text = space

  IMPORTING

   buffer = l_xspace.

CALL FUNCTION 'SCMS_STRING_TO_XSTRING'

  EXPORTING

   text = l_letter

  IMPORTING

   buffer = l_xletter.

CALL FUNCTION 'SCMS_STRING_TO_XSTRING'

  EXPORTING

    text  = l_text

  IMPORTING

    buffer = l_xstring.

REPLACE ALL OCCURRENCES OF l_xletter IN l_xstring WITH l_xspace IN BYTE MODE.

CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'

  EXPORTING

    buffer      = l_xstring

  IMPORTING

    output_length = lv_bin_outlen

  TABLES

    binary_tab   = gt_bin.

CALL FUNCTION 'SCMS_BINARY_TO_STRING'

  EXPORTING

    input_length = lv_bin_outlen

  IMPORTING

    text_buffer = l_text

  TABLES

    binary_tab  = gt_bin.

WRITE: / 'After:', l_text.

The code is colored by ABAP code lighter for SCN.sap.com.

Result:

Best regards,

George Shlyahov

6 REPLIES 6

former_member212002
Active Contributor
0 Kudos

Hello,

Have you tried FM SCMS_STRING_TO_FTEXT??

Thanks

Abhinab

former_member386202
Active Contributor
0 Kudos

Hi,

USe FM

call function 'SCMS_STRING_TO_XSTRING'
exporting
text     = html_data-line
importing
buffer   = xhtml_string
exceptions
failed  
= 1
others   = 2.


Regards,

Prashant    

GeorgeShlyakhov
Participant
0 Kudos

Hi Bhupinder!

Try to refer to the code below:


REPORT ztest.

DATA:

  l_text        TYPE string VALUE 'Some text ┐ some text',

  l_letter      TYPE string VALUE '┐',

  l_xspace      TYPE xstring,

  l_xletter     TYPE xstring,

  l_xstring     TYPE xstring,

  lv_bin_outlen TYPE i,

  gt_bin        TYPE STANDARD TABLE OF sdokcntbin.

WRITE: / 'Before:', l_text.

CALL FUNCTION 'SCMS_STRING_TO_XSTRING'

  EXPORTING

   text = space

  IMPORTING

   buffer = l_xspace.

CALL FUNCTION 'SCMS_STRING_TO_XSTRING'

  EXPORTING

   text = l_letter

  IMPORTING

   buffer = l_xletter.

CALL FUNCTION 'SCMS_STRING_TO_XSTRING'

  EXPORTING

    text  = l_text

  IMPORTING

    buffer = l_xstring.

REPLACE ALL OCCURRENCES OF l_xletter IN l_xstring WITH l_xspace IN BYTE MODE.

CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'

  EXPORTING

    buffer      = l_xstring

  IMPORTING

    output_length = lv_bin_outlen

  TABLES

    binary_tab   = gt_bin.

CALL FUNCTION 'SCMS_BINARY_TO_STRING'

  EXPORTING

    input_length = lv_bin_outlen

  IMPORTING

    text_buffer = l_text

  TABLES

    binary_tab  = gt_bin.

WRITE: / 'After:', l_text.

The code is colored by ABAP code lighter for SCN.sap.com.

Result:

Best regards,

George Shlyahov

0 Kudos

Hi George,

Thanks for the code. Issue is with this special character which i can't declare within the abap code.

I am getting the below syntax check priority 1 warning in ATC, due to which i will not be able to release the TR.

Characters with hexadecimal code <02> cannot be used in ABAP source text

so i want to replace the code for special character that help me to resolve this ATC check warning.

Best Regards,

Bhupinder

reinis_dzenis
Explorer
0 Kudos

You can use something like this to address your issue

(e.g. loop trough string and check hex code for each character):


DATA: text TYPE string.

DATA: current_pos TYPE i,

          text_length TYPE i.

DATA: charid TYPE cps_segtre-charid.



* Set text length

   text_length = STRLEN( text ).

* Loop through all characters in string

   current_pos = 0.

   WHILE current_pos < text_length.

*   Get hex code of current character

     PERFORM character_id_get

       USING text+current_pos(1)

       CHANGING charid.

IF charid = '000002'.

      CLEAR: text+current_pos(1).

ENDIF.

ENDWHILE.

*&---------------------------------------------------------------------*

*&      Form  character_id_get

*&---------------------------------------------------------------------*

FORM character_id_get

   USING char TYPE char01

   CHANGING charid TYPE cps_segtre-charid.

   FIELD-SYMBOLS <f> TYPE x.

* Claer return

   CLEAR: charid.

* Get hex code of given character

   ASSIGN char TO <f> TYPE 'X'.

   CASE cl_abap_char_utilities=>endian.

     WHEN 'B'.

       charid+0(2) = '00'.

       charid+2(2) = <f>+0(1).

       charid+4(2) = <f>+1(1).

     WHEN 'L'.

       charid+0(2) = '00'.

       charid+2(2) = <f>+1(1).

       charid+4(2) = <f>+0(1).

     WHEN OTHERS.

       ASSERT cl_abap_char_utilities=>endian CO 'BL'.

   ENDCASE.

ENDFORM.                    "character_id_get

0 Kudos

Hi All,

Thanks alot for your reply.

@George - With the help of your code, i am able to fix the issue. For special character, i created the constant attribute in the class and set the default value i.e. '02' which is corresponding hexadecimal code for special chracter to that attribute.

Thanks for youe help.

Regards,

Bhupinder