How to check string by HEX values of characters

Problem: you need to check text for the unwanted characters. Few of them are invisible characters. You need to check hex value of each character.

Solution: There is no standard solution for this problem. All of the standard SAP Function Modules remove special characters like regional characters. I have not found any FM that would remove invisible characters.

So, a non-standard solutions was needed. Below you will find code which removes all characters from the given hex range. Hexadecimal values are compared.

CONSTANTS:
  cv_hex_low TYPE x LENGTH 2 VALUE '0020',
  cv_hex_high TYPE x LENGTH 2 VALUE '007A'.
 
FIELD-SYMBOLS:
  <lv_field> TYPE ANY,
  <lv_hex_character> TYPE x.
 
DATA:
  lt_data TYPE TABLE OF zsome_structure,
  ld_data TYPE REF TO zsome_structure,
  lv_length TYPE i,
  lv_current TYPE i,
  lv_character TYPE char1,
  lv_field_num TYPE i.
 
LOOP AT lt_data REFERENCE INTO ld_data.
  clear lv_field_num.
  DO.
    lv_field_num = lv_field_num + 1.
 
    ASSIGN COMPONENT lv_field_num OF STRUCTURE ld_data->* TO <lv_field>.
    IF sy-subrc <> 0 OR <lv_field> IS NOT ASSIGNED.
      EXIT.
    ENDIF.
    CHECK <lv_field> IS NOT INITIAL.
 
    CLEAR lv_current.
    DESCRIBE FIELD <lv_field> LENGTH lv_length IN CHARACTER MODE.
    DO lv_length TIMES.
      lv_character = <lv_field>+lv_current(1).
      ASSIGN lv_character TO <lv_hex_character> CASTING.
      CHECK <lv_hex_character> IS ASSIGNED.
 
      IF <lv_hex_character> < cv_hex_low
        OR <lv_hex_character> > cv_hex_high.
        REPLACE ALL OCCURRENCES OF lv_character IN <lv_field> WITH ''.
        CONTINUE.
      ENDIF.
      lv_current = lv_current + 1.
    ENDDO.
  ENDDO.
ENDLOOP.
CONSTANTS:
  cv_hex_low TYPE x LENGTH 2 VALUE '0020',
  cv_hex_high TYPE x LENGTH 2 VALUE '007A'.

FIELD-SYMBOLS:
  <lv_field> TYPE ANY,
  <lv_hex_character> TYPE x.

DATA:
  lt_data TYPE TABLE OF zsome_structure,
  ld_data TYPE REF TO zsome_structure,
  lv_length TYPE i,
  lv_current TYPE i,
  lv_character TYPE char1,
  lv_field_num TYPE i.

LOOP AT lt_data REFERENCE INTO ld_data.
  clear lv_field_num.
  DO.
    lv_field_num = lv_field_num + 1.

    ASSIGN COMPONENT lv_field_num OF STRUCTURE ld_data->* TO <lv_field>.
    IF sy-subrc <> 0 OR <lv_field> IS NOT ASSIGNED.
      EXIT.
    ENDIF.
    CHECK <lv_field> IS NOT INITIAL.

    CLEAR lv_current.
    DESCRIBE FIELD <lv_field> LENGTH lv_length IN CHARACTER MODE.
    DO lv_length TIMES.
      lv_character = <lv_field>+lv_current(1).
      ASSIGN lv_character TO <lv_hex_character> CASTING.
      CHECK <lv_hex_character> IS ASSIGNED.

      IF <lv_hex_character> < cv_hex_low
        OR <lv_hex_character> > cv_hex_high.
        REPLACE ALL OCCURRENCES OF lv_character IN <lv_field> WITH ''.
        CONTINUE.
      ENDIF.
      lv_current = lv_current + 1.
    ENDDO.
  ENDDO.
ENDLOOP.

Komentowanie jest zakończone.