Binario to Hex

Asked

Viewed 81 times

1

I am developing a server that accepts connections via socket.
But the information received is in binary and during printing appear strange characters.

inserir a descrição da imagem aqui

I believe that the conversion or the way I read the data is not very correct (not to say totally wrong).
Below the code so far:

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class Servidor {
  public static void main(String[] args) throws Exception {

    int porta = 2952;

    ServerSocket m_ServerSocket = new ServerSocket(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 don’t have much intimacy with Java yet.
I would like possible solutions for this my impassable.

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

1 answer

2


There’s probably a simpler way to do it, but 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;
    }
}
  • Done. I did not seek another way. But I already have starting point for study. Thank you !

Browser other questions tagged

You are not signed in. Login or sign up in order to post.