Convert from Byte Array to String and compare against another String (java) -
Convert from Byte Array to String and compare against another String (java) -
i have udp server/client sends , receive information. client
send illustration "test"
server, server receive "test"
in byte[]
array representation , convert string
using string(byte[])
constructor. need compare new string
received against string using equalsignorecase
method. it's not working...
here's sample of 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 explanation string in sentence
not "test"
looks like "test"
when display it. strings are:
" test"
or "test "
"test\n"
but possible have homoglyph in either client string, or server code.
alright found out using sentence.length() sentence 1024 characters long...if take substring of it, sentence.substring(0,4).
ah. problem (probably) ignoring size of received packet, , including entire buffer array in string.
the right way extract string is:
new string(receivepacket.getdata(), receivepacket.getoffset(), receivepacket.getlength(), "utf-8");
... though think offset going zero, given previous statements.
(but possible client sending nul bytes ... in case, need alter things on client side.)
java arrays string bytearray
Comments
Post a Comment