4

I am currently programming a multi-player game, and I am working on the networking side of it all right now. I have a packet system set up, and the way it works (with Strings at least) is that it takes a number of characters to a maximum of "X" characters. The characters are converted to bytes for sending to the server. If there are less than X characters, then the remaining bytes are set to 0. The issue is that when processing this information on the server and converting it to a string, the 0-byte characters are a '□' in my console, and invisible in my JTextPane. How can I remove all of these 0-byte characters from the String in a clean way? I'd prefer not to have another loop and more variables just to remove the 0-bytes before converting to a String. No one likes dirty-looking code. :p

Packet Data:

03100101118000000000971001091051100000000
  • 03 = Packet ID (irrelevant)
  • 100101118000000000 = Username ("dev")
  • 971001091051100000000 = Password ("admin")

Resulting String:

  • usernameString = "dev□□□□□□□□□"
  • passwordString = "admin□□□□□□□"

What I've tried:

usernameString.replaceAll(new String(new byte[] {0}, "UTF-8"), "");
passwordString.replaceAll(new String(new byte[] {0}, "UTF-8"), "");

However, this did not change the String at all.

3
  • use some regex to math end of string being one or more 0 Commented May 8, 2015 at 2:06
  • 1
    If you are using Java 7 or higher, it's better to use StandardCharsets.UTF_8 constant instead of "UTF-8" literal. This is faster and you will not need to handle/rethrow UnsupportedEncodingException. Commented May 8, 2015 at 2:16
  • @TagirValeev Wow I never realized how much faster StandardCharsets.UTF_8 was compared to the "UTF-8" literal. Thanks a ton. Commented May 8, 2015 at 2:30

2 Answers 2

5

As all of your zeros appear at the end of the string you can solve this even without regular expresions:

static String trimZeros(String str) {
    int pos = str.indexOf(0);
    return pos == -1 ? str : str.substring(0, pos);
}

usernameString = trimZeros(usernameString);
passwordString = trimZeros(passwordString);
Sign up to request clarification or add additional context in comments.

2 Comments

I like this better than a regular expression assuming 0 only occurs at the end.
@TagirValeev Due to the nature of the packets, there will never be a 0-byte unless it's an unfilled character at the end. It's a great safety-precaution though.
5
usernameString = // replace 1 or more \0 at the end of the string
    usernameString.replaceAll("\0+$", "");

\0 is an escape for the Unicode character with a numerical value 0 (sometimes called NUL). In other words:

System.out.println((int) "\0".charAt(0)); // prints 0

What you tried seems to work for me too. (See Ideone example.) Not sure why it doesn't for you. There may be another problem. Make sure you are reassigning the result. ; )

3 Comments

That does not work for me because there is no '\' before the 0-byte character in the String, nor an actual '0'. The character is essentially null.
See my edit. Sorry, I suppose I could have explained it initially. \ does not appear in the String, it's an escape.
This worked fine for me in that case. I chose the other answer as the answer because it looks cleaner, but this code is functional and definitely gets the job done, thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.