Skip to Content

how to replace a hexcode in a string

I have a string that I read from a database that sometimes occurs special characters (e.g. HexCode A0). I'd like to remove all occurances of that character from the string.

I couldn't get "REPLACE" to work with HexCodes.

What can I do?

thx

Add a comment
10|10000 characters needed characters exceeded

Related questions

5 Answers

  • Best Answer
    Posted on Nov 19, 2004 at 03:50 PM

    Did you put in addition IN BYTE MODE?

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Nov 19, 2004 at 06:14 PM

    The following example worked for me. I had to use the debugger to get the special characters into my test string. If you are worried about Unicode, this should work in a Unicode system as well (assuming that your input string is Unicode). I am taking your A0 hex and replacing it with 20 (space).

    DATA: hex1(1)  TYPE x VALUE 'A0'.
    DATA: hex2(1)  TYPE x VALUE '20'.
    DATA: uhex1(2) TYPE x VALUE '00A0'.
    DATA: uhex2(2) TYPE x VALUE '0020'.
    DATA: char1(1) TYPE c.
    
    DATA: test_string TYPE string VALUE 'Stuff in here'.
    DATA: test_xstring TYPE xstring.
    
    ****Convert the Character String to Binary String
    CALL FUNCTION 'SCMS_STRING_TO_XSTRING'
      EXPORTING
        text   = test_string
      IMPORTING
        buffer = test_xstring.
    
    IF cl_abap_char_utilities=>charsize = 1.
      REPLACE ALL OCCURENCES OF hex1 IN test_xstring WITH hex2 IN BYTE MODE.
    ELSE.
      REPLACE ALL OCCURENCES OF uhex1 IN test_xstring WITH uhex2 IN BYTE MODE.
    ENDIF.
    
    CLEAR test_string.
    
    DATA:
      convin  TYPE REF TO cl_abap_conv_in_ce.
    CALL METHOD cl_abap_conv_in_ce=>create
      EXPORTING
        input = test_xstring
      RECEIVING
        conv  = convin.
    
    CALL METHOD convin->read
      IMPORTING
        data = test_string.
    
    WRITE: / test_string.
    

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Nov 23, 2004 at 04:29 AM

    replace characters by ' '/ Space.

    1. Get the string lenght of ur field

    wf_datalen = strlen( <String> ).

    2. DO wf_datalen TIMES.

    wf_pos = sy-index - 1.

    wf_char = <String>+wf_pos.

    IF wf_char CN

    'BCDEFGHIJKLMNOPQRSTUVWXYZ 123456789' &

    'abcdefghijklmnopqrstuvwxyz+-*/=~#^()[]\&%$@.,;:"!'.

    wf_dataline+wf_pos(1) = Space.

    ENDIF.

    Enddo.

    I have removed A & 0(Zero) from the above. So that it will be replaced by space in your case.

    Regards,

    Prabhu Rajesh.

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Nov 19, 2004 at 07:28 PM

    You might also have a look at function module SCP_REPLACE_STRANGE_CHARS. It might work for what you are wanting to do.

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Nov 25, 2004 at 05:43 AM

    Hi Daniel,

    Have you tried casting the both the string as well as the value to Characters using field symbols.

    Then I believe REPLACE would work.

    To achieve the required casting Using assing with Casting.

    Regards,

    Pavan

    Add a comment
    10|10000 characters needed characters exceeded

    • Former Member Daniel Humberg

      Hi Daniel,

      If I understand u problem correctly, you read a character string from database which sometimes has characters with hex code '0A' displayed as '#' which you would like to replace.

      Now try this:

      v1 is the string

      v2 type x value '0A'.

      field-symbols: <f2> type c.

      assign v2 to <f2> casting.

      replace all occurrences of <f2> in v1 with space.

      You may have some problem doing this assignment sometimes, if so declare v2 as v2(4) type x value '0A'.

      Now change the replace as

      replace all occurrences of <f2>+0(1) in v1 with space.

      Hope this helps.

      Pavan

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.