title: Mangled Hex message via socket
body: <p>I am trying to pass a hex message from a C server to a Java client.
  The communication works. But the hex value that I get on Java client seems to be appended with "ff". Why is this happening?</p>
  
  <p>At the C side,when I print the bytes I want to send(in Hex), they seem ok.</p>
  
  <p>Please see the code below:</p>
  
  <p>C server:</p>
  
  <pre><code>                    int datalen = 220;
  
                      /* create  outgoing message */
                      idx = 0;
                      en_outmsg[idx++] = FEB &amp; 0xFF;
                      en_outmsg[idx++] = ENT &amp; 0xFF;
                      en_outmsg[idx++] = CBA &amp; 0xFF;
                      en_outmsg[idx++] = GRP &amp; 0xFF;
                      en_outmsg[idx++] = OUTGOING &amp; 0xFF;
                      en_outmsg[idx++] = (datalen &amp; 0xFF00) &gt;&gt; 8;
                      en_outmsg[idx++] = datalen &amp; 0xFF;
  
                      for(i= 0; i&lt;39; i++){
                      printf("en_outmsg[%d] to send = %x\n",i, en_outmsg[i]);
                      }
                      en_outmsg[i+1] = '\n';
  
  
                      if (send(connected, en_outmsg, 40, 0) &gt; 0)
                      {
                          printf("sending over\n");
  
                      }
  </code></pre>
  
  <p>Java Client:</p>
  
  <pre><code>    while( (bytes =dis.read(buffer, 0, 40)) != -1){
          for(int index=38; index &gt;= 0; index--)  {           
  
                  System.out.println("index ="+index);
                  System.out.println("buffer ="+Integer.toHexString(buffer[index]));
          }
          System.out.println("bytes="+bytes); 
          len = 0;
          len |= buffer[5];
          len = len &lt;&lt; 8;
          len |=  buffer[6];
          System.out.println("value of len= "+len);
      }
  </code></pre>
  
  <p>OutPut:
  value of len= -36
  buffer[5]=0
  buffer[6]= 0xfffffffdc</p>
  
  <p>UPDATED</p>
  
  <p>Here is my wireshark output(This is what C server pushes to Java Client):</p>
  
  <p>Note that at row 5 "00 dc" corresponds to datalen= 220 which should be stored as such in len on Java client. So there is clearly some error on the Java Client. Like you have all mentioned, I can use Integer.toHexString(((int)buffer[index]) &amp; 0xFF) for printing. But I need to store the byte array with the correct hex values. Please help</p>
  
  <pre><code>0000  00 00 03 04 00 06 00 00  00 00 00 00 00 00 08 00   ........ ........
  0010  45 00 00 5c b4 75 40 00  40 06 a0 ac c0 a8 b2 14   E..\.u@. @.......
  0020  c0 a8 b2 14 09 60 b7 bb  fe bd 3a 2d fe 36 cc 8c   .....`.. ..:-.6..
  0030  80 18 02 20 e5 c8 00 00  01 01 08 0a 00 04 8e 5f   ... .... ......._
  0040  00 04 8e 5f 0a 01 01 16  01 00 dc 00 01 02 03 04   ..._.... ........
  0050  05 06 07 08 09 0a 0b 0c  0d 0e 0f 2b 7e 15 16 28   ........ ...+~..(
  0060  ae d2 a6 ab f7 15 88 09  cf 4f 3c d0               ........ .O&lt;.    
  </code></pre>