Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 985
Thomas F Avatar asked Oct 31 '14 07:10

Thomas F


People also ask

How do you convert a byte array into a string?

There are two ways to convert byte array to String: By using String class constructor. By using UTF-8 encoding.

Can we convert byte to string in java?

Given a Byte value in Java, the task is to convert this byte value to string type. One method is to create a string variable and then append the byte value to the string variable with the help of + operator. This will directly convert the byte value to a string and add it in the string variable.

Can we convert string to byte array in java?

We can use String class getBytes() method to encode the string into a sequence of bytes using the platform's default charset. This method is overloaded and we can also pass Charset as argument. Here is a simple program showing how to convert String to byte array in java.

How do you convert Bytearray?

Convert byte[] to String (text data) toString() to get the string from the bytes; The bytes. toString() only returns the address of the object in memory, NOT converting byte[] to a string ! The correct way to convert byte[] to string is new String(bytes, StandardCharsets. UTF_8) .


2 Answers

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.)

like image 194
Stephen C Avatar answered Sep 17 '22 09:09

Stephen C


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 i.e. 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);
like image 33
Pratik Avatar answered Sep 18 '22 09:09

Pratik