cancel
Showing results for 
Search instead for 
Did you mean: 

how to replace a hexcode in a string

daniel_humberg
Contributor
0 Kudos

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

Accepted Solutions (1)

Accepted Solutions (1)

nablan_umar
Active Contributor
0 Kudos

Did you put in addition IN BYTE MODE?

daniel_humberg
Contributor
0 Kudos

I don't want to do it in BYTE MODE since i am not sure if this would work on Unicode systems....

nablan_umar
Active Contributor
0 Kudos

Hi Daniel,

I was able to replace a hexcode but the hex field that I compared with is of class attributes cl_abap_char_utilities=>horizontal_tab. Can you post your code here.

Answers (4)

Answers (4)

thomas_jung
Developer Advocate
Developer Advocate

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.

Former Member
0 Kudos

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

daniel_humberg
Contributor
0 Kudos

Hey PrabhuRajesh,

i think your solution would work for the characters 'A' and '0', but not for the hexcode 'A0'.

Hey Pavan,

maybe you could add some code to your proposal. That would make it easier for me to understand...

Former Member
0 Kudos

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

Former Member
0 Kudos

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.

thomas_jung
Developer Advocate
Developer Advocate
0 Kudos

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