0

How to implement an unsigned byte in java, restriction being the size should be 1 byte only i.e. we cannot convert to short or int by And' ing with 0xFF

I have to transfer an unsigned byte array through the socket as recieving end is a C code method expecting an unsigned byte array of size 1 byte only, But since Java doesnt support the concept of unsigned byte the problem occurs. Do we have any means to achieve this.

byte b=(byte)0xF0; or even byte b1=0x00; is not properly send over socket channel. Please see the method for writting to the server.

public void encode(IoSession session, Object message, ProtocolEncoderOutput out) throws Exception {

    //lProxylogger.info("Inside ProtocolEncoderAdapter. Encoding response to client..");
    String response ;

    //lProxylogger.info("The response length in encode adapter is "+ response.length()+" Message="+response);
    byte[] responseStream; 

    response=(String) message;

    responseStream= response.getBytes("windows-1252");

    //responseStream=serialize(message);

     IoBuffer buffer = IoBuffer.allocate(responseStream.length);
    // buffer.putInt(response.length);
     //IoBuffer buffer=(IoBuffer)message;
      // buffer.putObject(message);
     System.out.println("Encoded Response:"+new String(responseStream));
     for(byte b:responseStream)
         System.out.print(b+",");
     System.out.println();
     buffer.put(responseStream);
//     lProxylogger.info("the response buffer size is "+ buffer.capacity());
     buffer.setAutoShrink(true);
    buffer.shrink();
  //  lProxylogger.info("After shrinking the buffer size is "+ buffer.capacity());
     buffer.flip();

    //System.out.println("Writing response to Stream..");
    out.write(buffer);

}
CodeFReak
  • 1
  • 2

2 Answers2

5

Signed-ness is just in how you interpret the 8 bits of a byte. It is neither longer nor shorter by being signed, and is not somehow different to transmit. You just send the byte. It's up to anything that interprets that byte to treat it as unsigned if it's unsigned, but this is nothing to do with representing it as 8 bits or sending it.

Sean Owen
  • 66,182
  • 23
  • 141
  • 173
  • Thank you Sean for replying, When I tried to send 0xF0 (-16 ASCII converted) or even 0x00 through socket, The value has not been decoded correctly (Some Junk) through socket end. I am using Java code, The recieving end is implemented in C and I am using the Redhat Linux platform. – CodeFReak Apr 08 '13 at 05:45
  • As you say, it's an issue in "decoding" (interpreting as signed) in the C code. It is unrelated to the sending/encoding side in Java. – Sean Owen Apr 08 '13 at 05:51
  • Exactly but my objective is to send some values like 0xF0,0x00 to through the socket to C coded server which is expecting unsigned byte. I can recieve and decode all values from the server correctly but when I tried sending these values, It reports some junk rather than expected from the recieving end. Here is my implementation to send the data to server (out.write method writes to server finally) – CodeFReak Apr 08 '13 at 05:55
  • Not sure what you mean. The byte with bits 11110000 is neither signed nor unsigned. You can write it as (byte) 0xF0 in Java. Maybe you are not actually sending that. Your code does not show this. – Sean Owen Apr 08 '13 at 06:07
  • My apologies for not being clear, Let's take a scenario where byte [] b={48,0x00,(byte)0xF0}, I want to transmit b over socket, On the recieving end only 48 has been recieved properly but none 0x00 or 0xF0 – CodeFReak Apr 08 '13 at 06:27
  • What was received? You're not saying what you see or showing the code with the problem. It's on the receiving end from what you describe. – Sean Owen Apr 08 '13 at 06:37
  • Sending end {48,48,(byte)0xF0,0x00} (Expected String output:00ð0). Recieving End {48,48} (Recieved String is : 00) – CodeFReak Apr 08 '13 at 06:44
0

It appears your choice of character encoding could be corrupting your String. You shouldn't attempt to place binary in text if you also use an encoding like this.

I suggest you simplify your code. Instead try

String response=(String) message;
for(char ch : response.toCharArray()) {
    System.out.println(Integer.toHexString(ch) + ", ");
    out.write(ch);
}

If this works I suggest trying an ISO-8859-1 encoding which basically does the same thing.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • Also changing to char Array will promote the size to 2 byte while the server expects 1 byte for that which will lead to overflow. I tried diffrent encodings but Changing to ISO-8859-1 will also not work as it is a subset of windows-1252. – CodeFReak Apr 08 '13 at 08:14
  • ISO-8859-1 encodes all characters which can be represented as one byte without change. This will take binary in a string without messing with it. – Peter Lawrey Apr 08 '13 at 12:50
  • It is not clear to me how you expect encoding to work as windows-1252 will turn some characters into one or two bytes and visa versa. This means you might not get 4 characters from 4 bytes. – Peter Lawrey Apr 08 '13 at 12:53
  • The reason to use windows-1252 was due to the following facts, - Same code when I tried using windows OS (windows-1252 default encoding) everything works fine - Linux was having default encoding of UTF-8 so I changed it to windows default but it didnt worked out. – CodeFReak Apr 09 '13 at 03:43