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

Converting control characters to spaces in a Unicode program?

Former Member
0 Likes
1,964

I want to take an ASCII character string and convert any

ASCII Control Characters to Spaces.

In a non-Unicode program, I define the following hex constant:

CONSTANTS: c_control_to_space(64) TYPE x VALUE

'00200120022003200420052006200720082009200A200B200C200D200E200F20' &

'10201120122013201420012016201720182019201A201B201C201D201E201F20'.

I then execute the following TRANSLATE statement:

TRANSLATE w_transcript USING c_control_to_space.

What would be the "approved" method of accomplishing the same effect

in a Unicode program?

I want to take an ASCII character string and convert any

ASCII Control Characters to Spaces.

In a non-Unicode program, I define the following hex constant:

CONSTANTS: c_control_to_space(64) TYPE x VALUE

'00200120022003200420052006200720082009200A200B200C200D200E200F20' &

'10201120122013201420012016201720182019201A201B201C201D201E201F20'.

I then execute the following TRANSLATE statement:

TRANSLATE w_transcript USING c_control_to_space.

What would be the "approved" method of accomplishing the same effect

in a Unicode program?

5 REPLIES 5
Read only

former_member186741
Active Contributor
0 Likes
1,535

you can use the constants in cl_abap_char_utilities to convert the usual ones (HORIZONTAL_TAB etc) but your code is a bit different, all it doing is converting hex values of 00 - 1F to x'20' which is spaces. (Although there looks to be a slight bug in it if you look at this fragment '....142001201620....' after 1420 there is 0120 which is already catered for, I think it should actually be 1520).

You can use something like this instead:

Data x type x.

Field-symbols <hex> type x.

Field-symbols <char> type c.

Assign X to <hex> .

Assign <hex> to <char> casting.

Do 32 times.

X = sy-index - 1.

replace all occurrences of <char> in w_transcript with ''.

Enddo.

Read only

0 Likes
1,535

Neil,

First, thank you for pointing out my typo. You are correct that the "0120" in the second line of the literal was intended to be "1520".

Second, thank you for your suggestion. Based on your idea, I tried something similar, but not exactly what you suggested. In particular, since I can't figure out how to construct the constant that I want, I used your idea to construct it as a variable, as follows:

DATA number TYPE i.

DATA offset TYPE i.

DATA hex(4) TYPE x.

FIELD-SYMBOLS <char> TYPE c.

ASSIGN hex TO <char> CASTING TYPE c.

DATA w_control_to_space(64) TYPE c.

DO 32 TIMES.

hex = sy-index - 1.

offset = 2 * ( sy-index - 1 ).

number = STRLEN( <char> ).

IF number GT 1.

SUBTRACT 1 FROM number.

SHIFT <char> LEFT BY number PLACES.

ENDIF.

w_control_to_space+offset(1) = <char>.

ENDDO.

After having constructed "w_control_to_space", I can now use the TRANSLATE statement:

TRANSLATE w_transcript USING w_control_to_space.

This code passes the Unicode syntax checks and works correctly on a non-Unicode system. I don't have access to a Unicode system on which to run it. I'd appreciate any feedback on this approach - especially if someone can actually test it on a Unicode system.

Read only

0 Likes
1,535

Try this code to replace all occurances of control chars between 00 to 1F. I don't have unicode system to test this.

data: len type i,
      off type i,
      hex(2) type x value '001F'. "1F00 on Windows box

data asci_str(100) type c.

field-symbols <fs> type any.

len = strlen( asci_str ).

while off lt len.
  assign asci_str+off(1) to <fs> casting type x.
  if <fs> le hex.
    asci_str+off(1) = ' '.
  endif.
  add 1 to off.
endwhile.

Regards

Sridhar

Read only

0 Likes
1,535

Sridhar K included the line

hex(2) type x value '001F'. "1F00 on Windows box

which points out that I didn't account for the "endian"ness of a Unicode system. Does the following version work correctly on all of non-Unicode, Big-endian Unicode, and Little-endian Unicode systems?

CLASS cl_abap_char_utilities DEFINITION LOAD.

DATA offset TYPE i.

DATA hex(4) TYPE x.

FIELD-SYMBOLS <char> TYPE c.

ASSIGN hex TO <char> CASTING TYPE c.

DATA w_control_to_space(64) TYPE c.

DO 32 TIMES.

hex = sy-index - 1.

offset = 2 * ( sy-index - 1 ).

CASE cl_abap_char_utilities=>charsize.

WHEN 1.

SHIFT hex LEFT BY 3 PLACES IN BYTE MODE.

WHEN 2.

IF cl_abap_char_utilities=>endian EQ 'B'.

SHIFT hex LEFT BY 2 PLACES IN BYTE MODE.

ELSE.

SHIFT hex LEFT BY 3 PLACES IN BYTE MODE.

ENDIF.

ENDCASE.

w_control_to_space+offset(1) = <char>.

ENDDO.

TRANSLATE w_transcript USING w_control_to_space.

Read only

0 Likes
1,535

Works on Non-UC, and i'm expectiong it should work on other systems too.

Regards

Sridhar