0

I am currently sending a String to the server and I am getting ASCCII characters when iterating through the buffer on the server side. How can I send the String to receive it as hexadezimal on the server?

code

String message;
String result;
String aString = "#2016011400000060.00#010104#004500##";
// String aString = "###";
BufferedReader inFromUser = new BufferedReader(new StringReader(aString));

Socket clientSocket = new Socket("localhost", 1000);
DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
message = inFromUser.readLine();
outToServer.writeBytes(message + '\n');
result = inFromServer.readLine();
System.out.println(result);
clientSocket.close();
TheBook
  • 1,698
  • 1
  • 16
  • 32

1 Answers1

1

Possible duplicate:

Converting A String To Hexadecimal In Java

public String toHex(String arg) {
    return String.format("%040x", new BigInteger(1, arg.getBytes(/*YOUR_CHARSET?*/)));
}
Community
  • 1
  • 1
Tadija Bagarić
  • 2,495
  • 2
  • 31
  • 48
  • but how can I send it with the socket since there is no writeHexa() method? – TheBook Sep 26 '16 at 19:43
  • @TheBook Call `writeBytes(toHex(message + '\n'));` – Andreas Sep 26 '16 at 19:46
  • @TheBook That is because there is no HEX data type in Java. Is unclear what you're trying to do. – Titus Sep 26 '16 at 19:48
  • Thanks sir for your answer. but is not the parameter arg is my message? what do you mean with `YOUR_CHARSET`? – TheBook Sep 26 '16 at 19:49
  • @TheBook You can specify the charset via the [`StandardCharsets`](http://docs.oracle.com/javase/7/docs/api/java/nio/charset/StandardCharsets.html) class. For example you can give StandardCharsets.UTF_8 as the parameter. – Tadija Bagarić Sep 27 '16 at 01:48