2

I am trying to pass a String value in socket to another server. The other server should receive the value in hex format.

i.e If my String is s = "600185838e" at the server it should receive as 60 01 85 83 8e, but these values what I sent are been converted to ASCII & is not in the desired format.

I am using socket connection

BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(this.socket.getOutputStream()));

wr.write(messageBody);

wr.flush(); 

How can I send my String value similar as Hex value?

Thanking you all in advance

MByD
  • 135,866
  • 28
  • 264
  • 277
Vardhaman
  • 899
  • 3
  • 11
  • 10

2 Answers2

7

You should convert the hex string to byte array and then send it as byte array:

OutputStream out = this.socket.getOutputStream();
out.write(yourBytearray);

This is the method for converting the hex string to byte[] this is a copy from the link I gave, but I copied it here to make clear what I'm talking about:

public static byte[] hexStringToByteArray(String s) {
    int len = s.length();
    byte[] data = new byte[len / 2];
    for (int i = 0; i < len; i += 2) {
        data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
                             + Character.digit(s.charAt(i+1), 16));
    }
    return data;
}
Community
  • 1
  • 1
MByD
  • 135,866
  • 28
  • 264
  • 277
  • Hi I tried the same, String s="600185838e" converted to hexString the value is = "36303031383538333865". & send this hexString as a messageBody... but at the socket server this is giving ASCII value. – Vardhaman Apr 05 '11 at 03:38
  • @Vardhaman - don't convert it to hex string, convert it to byte array, as described in the link I posted. – MByD Apr 05 '11 at 03:41
  • Hi MbyD, I just tried the same, but at the server they are reciving it as for 600185838e as 6-->36 0-->30 0-->30 ......e-->65 (i.e is again in ASCII) , the server should read it as 60 01 85 .... 8e – Vardhaman Apr 05 '11 at 04:13
  • Did you check what is the byte array being sent (take a look at it before you actually send it)? – MByD Apr 05 '11 at 04:27
  • I just tried I printed the bytearray in loop , its like this format 54 48 48 49 56 53 56 51 56 101 0035_0032 – Vardhaman Apr 05 '11 at 04:36
  • Hi MByD, Thank you I was able to send the message in the desired format after implementing the method given by you. – Vardhaman Apr 05 '11 at 06:39
  • @Vardhaman: that proves that your problem statement is incorrect. You want to send binary, not hex. – user207421 Apr 05 '11 at 08:54
0

You can also try splitting the string into 2-character long Strings and convert them to individual bytes via Byte.parseByte(my2CharString , 16) and then send them

Here's some code that will do what you want, just replace System.out.println() with write() <- must write only one byte:

String output = "ffee101";
    while(output.length() > 0){
        String byteToWrite;
        if(output.length() <= 2){
            byteToWrite = output;
            output = "";
        }
        else{
            byteToWrite = output.substring(0,2);
            output = output.substring(2);
        }
        byte b = (byte)Short.parseShort(byteToWrite, 16);
        System.out.println(b);
    }
Duncan
  • 980
  • 6
  • 17
  • Hi Duncan, getting confused ... you mean to say 6001 ... split it to 60 01... & run the same in loop.. but that sending data would run in a loop condition ? Could you help me to provide a code snippet. I tried the same only for String s="8e" getting Error java.lang.NumberFormatException: Value out of range. Value:"8e" Radix:16 – Vardhaman Apr 05 '11 at 04:23
  • oops, sorry. I forgot parseByte deals with signed numbers (java is silly). Try typecasting: (byte)Short.parseShort(my2CharString , 16) – Duncan Apr 05 '11 at 04:39
  • also, you should probably use an OutputStream so you can write bytes, not ints or Strings – Duncan Apr 05 '11 at 04:46
  • Hi Duncan, tried the as you suggested i.e tried on "8e". System.out.println("byte:-"+(byte)Short.parseShort("8e",16)); the output is -114 – Vardhaman Apr 05 '11 at 04:48
  • -114 is correct. Like I said, java does everything as signed numbers. Therefore, the value of anything over 0x80 will be negative (starting with -128 and going to -1 as 0xFF) – Duncan Apr 05 '11 at 04:55
  • Thank you Duncan, Here I have a doubt would it be nice to run in a loop condition the write, as this is a web app. Actually the e.g I provide was a sample , but the message length would be much more larger than it. – Vardhaman Apr 05 '11 at 08:03