I send requests from a client socket to a server socket and i want to differenciate requests(send as a byte array) using a escape character("\n"). I want to have one request per new line exemple :
"Request1 "
"Request2"
"Request3"
In order to do this , i need to convert the "\n" in byte in order to compare the requests like this
byte[] request= new byte[1024];
int nextByte;
while((nextByte=in.read(request))!=DELIMITER)
{
String chaine = new String( request,0,nextByte);
System.out.println("Request send from server: " + chaine);
}
The problm is that i get an number format exception when i am trying to convert "\n" in byte
private static final byte DELIMITER = Byte.valueOf("\n");
Thank you very much
Try this:
private static final byte DELIMITER = (byte) '\n';
Double quotes are for String literals, single quotes for characters, and Byte#valueOf does something else than what you think it does.
If you wanted to turn a String into bytes, you'd do:
byte[] theBytes = "\n".getBytes("UTF-8");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With