Binary for Hex

1

I'm developing a server that accepts socket connections. However, the information received is binary and strange characters appear during printing.

IthinktheconversionorthewayIreadthedataisnotverycorrect(nottosaytotallywrong).Belowthecodesofar:

importjava.io.BufferedReader;importjava.io.InputStreamReader;importjava.io.OutputStreamWriter;importjava.io.PrintWriter;importjava.net.ServerSocket;importjava.net.Socket;publicclassServidor{publicstaticvoidmain(String[]args)throwsException{intporta=2952;ServerSocketm_ServerSocket=newServerSocket(porta);System.out.print("ESCUTANTO PORTA : "+porta+"\n\n");

    int id = 0;
    while (true) {
      Socket clientSocket = m_ServerSocket.accept();
      ClientServiceThread cliThread = new ClientServiceThread(clientSocket, id++);
      cliThread.start();
    }
  }
}

class ClientServiceThread extends Thread {
  Socket clientSocket;
  int clientID = -1;
  boolean running = true;

  ClientServiceThread(Socket s, int i) {
    clientSocket = s;
    clientID = i;
  }

  public void run() {
    System.out.println("CONECTOU : ID - " + clientID + " - " + clientSocket.getInetAddress().getHostName());
    try {
      BufferedReader   in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
      PrintWriter   out = new PrintWriter(new OutputStreamWriter(clientSocket.getOutputStream()));
      while (running) {
        String clientCommand = in.readLine();
        System.out.println("ENVIOU :" + clientCommand);
        if (clientCommand.equalsIgnoreCase("quit")) {
          running = false;
          System.out.print("Stopping client thread for client : " + clientID);
        } else {
          out.println(clientCommand);
          out.flush();
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}


As I do not have much intimacy with Java yet. I would like possible solutions to this problem.

I have in mind that I should convert the binary value to Hexa, but I have not yet achieved such a feat.

    
asked by anonymous 16.11.2015 / 20:02

1 answer

2

It probably has a simpler way of doing this, but so at least I know it works.

public void run() 
{
    System.out.println("CONECTOU : ID - " + clientID + " - " + clientSocket.getInetAddress().getHostName());
    try 
    {
        BufferedReader   in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
        PrintWriter   out = new PrintWriter(new OutputStreamWriter(clientSocket.getOutputStream()));
        while (running) {
            String clientCommand = in.readLine();
            char[] temp = clientCommand.toCharArray();
            String hex = "";
            for(int i = 0; i < temp.length; i++)
            {
                hex += toHex(temp[i])+ " ";
            }
            System.out.println("ENVIOU :" + hex);
            if (clientCommand.equalsIgnoreCase("quit")) 
            {
                running = false;
                System.out.print("Stopping client thread for client : " + clientID);
            } 
            else 
            {
                out.println(clientCommand);
                out.flush();
            }
        }
    } 
    catch (Exception e) 
    {
        e.printStackTrace();
    }
}

public static String toHex(char c)
{
    String res = hex((byte)(c >> 4));
    return res + hex((byte)(c & 0x0F));
}

public static String hex(byte b)
{
    switch(b)
    {
        case 10: return "A";
        case 11: return "B";
        case 12: return "C";
        case 13: return "D";
        case 14: return "E";
        case 15: return "F";
        default: return ""+b;
    }
}
    
17.11.2015 / 00:32