Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

DatagramPacket.getData() weirdness  RSS feed

Alex Kirk
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
While working with a Datagram-based application, I've noticed what looks like a quirk in the getData() method for DatagramPackets. I'd been having all kinds of troubles comparing a byte array I'd created from a String to the byte array coming from an inbound DatagramPacket, where I knew exactly what the DatagramPacket should contain. When I looked a little closer, I found that although the printed Strings seemed equal, the byte array coming in was actually one byte longer, containing an odd little character at the end.
Basically, when I looked at this character -- either as System.out.println(array[9]), Byte.toString(), or Byte.intValue() -- it showed up as a "0". I figured I could solve my comparison problem by adding a "0" to the end of my home-grown String; when that didn't do it, I figured it was a null character, or "\0", and tried that. Neither worked.
Just to confound the issue further, when a String is created from the byte array generated from DatagramPacket.getData(), this last character doesn't print.
Any ideas what this character might be, and why it's there? Is this a problem with getData(), or something going on with the program sending me my DatagramPacket?
Alex Kirk
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!