Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

parse escape character as a byte

Tags:

java

sockets

byte

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

like image 437
ulquiorra Avatar asked Dec 20 '22 16:12

ulquiorra


1 Answers

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");
like image 97
Thilo Avatar answered Dec 29 '22 12:12

Thilo