简体   繁体   中英

Convert from Byte Array to String and compare against another String (java)

I have a UDP server/client that sends and receive information. The Client will send example "TEST" to the server, the server will receive "TEST" in a byte[] array representation and convert it to String using the String(byte[]) constructor. I then need to compare the new String received against another string using the equalsIgnoreCase method. But it's not working...

Here's a sample 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?

The most likely explanation is that the string in sentence is not "TEST" but something that looks like "TEST" when you display it. The most likely strings are:

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

But it is also possible that you have a homoglyph in either the client string, or your server code.


Alright so I just found out using sentence.length() that sentence is 1024 characters long...if I take a substring of it, specifically sentence.substring(0,4).

Ah. So the problem is (probably) that you are ignoring the size of the received packet, and including the entire buffer array in the string.

The most correct way to extract the string is:

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

... though I think that the offset is going to be zero, given the previous statements.

(But it is also possible that the client is sending the NUL bytes ... in which case, you will also need to change things on the client side.)

Create string object like this way

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

instead of

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

In your code will getting the data but it have extra content too. For example in your receiveData size set to 10 which will be reading from your packet object. Now for "Test" it will be use 4 block in your receiveData and rest of block ie 6 are null so that all will be use in your sentence object too. Thats why sentence will not match.

For test just print your sentence object first like this way

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

I think receiveData is longer than 4, so trailing bytes are also contained in the converted String. You should cut off the trailing bytes(byte 0) when you new String.

"TEST " and "TEST" are not equal. Verify string length.

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

To remove spaces using:

sentence = sentence.trim();

From Java tutorial use:

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM