Converting from a byte array to a string and comparing with another string (java)

I have a UDP server / client that sends and receives information. Client will send the "TEST" example to the server, the server will receive "TEST" in the byte[] array view and convert it to String using the String(byte[]) constructor String(byte[]) . Then I need to compare the new String obtained with another string using the equalsIgnoreCase method. But it does not work ...

Here is an example of my code:

 DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); serverSocket.receive(receivePacket); //Client sent "TEST" String sentence = new String(receivePacket.getData(), "UTF-8"); System.out.println("RECEIVED: " + sentence); System.out.println(sentence.equalsIgnoreCase("TEST")); //This gives me FALSE 

Any ideas?

+5
source share
5 answers

The most likely explanation is that the line in sentence not "TEST" , but that it looks like "TEST" when it is displayed. The most likely lines are:

  • " TEST" or "TEST "
  • "TEST\n"

But it is also possible that you have homoglyph both in the client line and in the server code.


Ok, so I just found out using the .length () clause that the sentence is 1024 characters long ... if I take a substring, in particular the .substring (0.4) sentence.

Oh. Therefore, the problem (possibly) is that you ignore the size of the received packet and include the entire buffer array in the line.

The most correct way to extract a string:

 new String(receivePacket.getData(), receivePacket.getOffset(), receivePacket.getLength(), "UTF-8"); 

... although I think the bias will be zero, given the previous statements.

(But it is also possible that the client sends NUL bytes ... in this case, you will also need to make a difference on the client side.)

+5
source

Create a string object this way

 String msg = new String(receiveData, 0, receivePacket.length); 

instead

 String sentence = new String(receivePacket.getData(), "UTF-8"); 

Your code will receive data, but it has additional content. For example, your receiveData size is set to 10, which will be read from your batch object. Now for "Test" 4 blocks in your getData will be used, and the rest of the ie 6 block will be null, so everything will be used in your proposal object. That is why the proposal does not match.

For a test, just first print your proposal object like this

 Log.e("Sentence",""+sentence); 
+1
source

I think that receiveData is longer than 4, so trailing bytes are also contained in the converted string. You must disable trailing bytes (byte 0) on a new line.

+1
source

"TEST " and "TEST" not equal. Check the string length.

 System.out.println(sentence.length()); 

To remove spaces using:

 sentence = sentence.trim(); 
0
source

From the Java tutorial :

 String received = new String(packet.getData(), 0, packet.getLength()); 

to get the string from the received packet; in your case you will have:

 String sentence = new String(receivePacket.getData(), 0, receivePacket.getLength()); 
0
source

Source: https://habr.com/ru/post/1205927/


All Articles