How to replace char in string using hex

bahriesz
bahriesz used Ask the Experts™
on
Hi..

I have a problem here. I have a string that have some characters that are not from normal ascii characters, e.g NUL, SOH (start of heading), STX (start of text), EXT (end of text), CR (carriage return), LF (line feed), etc.

I need to replace all those chars with space characters. I am thinking of using hex code. e.g replace 0 with 20 (0 - hex code for nul, 20 hex code for space). How can i do this?

Is there any way for me to replace those characters in my string with space.?

Tq
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Here is the example:

public class Replace{
    public static void main(String[] args){
        StringBuffer buffer = new StringBuffer(32);
        for(char ch=0; ch<0x20; ++ch){
            buffer.append(ch);
        }
        String test = buffer.toString();
        String result = test.replace('\u0006','\u0066');
        for(int i=0; i<0x20; ++i){
            test = test.replace((char)i,(char)(i+0x30));
        }
        System.out.println(test);
    }
}

Commented:
With java 1.4 you can use String.replaceAll.

Anyway, looks like you what to perform an URL-encoding (e.g. put your String in to a URL for a HTTP-GET). There is a class java.net.URLEncoder for perfoming this task and URLDecoder for perfoming the inverse transformation.
bahriesz:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.

Commented:
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

- Points for bazarny

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Venabili
EE Cleanup Volunteer

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial