Converting Byte Array to Int Array to send over java socket -


hi new java development , have following problem.

i construct char array values {130,56,0,0,2,0,0,0}. pass values string using string.valueof(), once done convert sting byte array using getbytes() function.

i use dataoutputstream writer write data socket. problem lies in fact on tcp level using wireshark trace data actuality sending c2 82 38 00 00 02 00 00 00 on wire , not original 130 56 0 0 2 0 0 0 .

code snippet below

public void run() {         system.out.println("got client !");         try {             // accept loop while connected             while (true) {                 byte[] arroutut = new byte[4096];                 int count = clientsocket.getinputstream().read(arroutut, 0, 4096);                 string clientrequest = "";                 system.out.println("packet size  " + count + "\n");                 if (count != -1) {                     (int outputcount = 0; outputcount < count; outputcount++) {                         char response = (char) arroutut[outputcount];                         clientrequest = clientrequest + response;                     }                     system.out.println("got clientrequest "+ clientrequest + "\n");                     count = 0;                  } else {                     break;                 }                  char[] map = new char[]{130,56,0,0,2,0,0,0};                  string stringmsg = string.valueof(map, 0, map.length);                  byte[] data = stringmsg.getbytes();                 byte[] asciidata = stringmsg.getbytes("ascii");                  system.out.println("byte data "+ data);                  system.out.println("byte data acii" + asciidata);                  outputstream dout = clientsocket.getoutputstream();                 dataoutputstream dos = new dataoutputstream(dout);                 int senddatalength = data.length;                  dos.write(data, 0, senddatalength);                 dos.flush();               }             clientsocket.close();             } catch (sockettimeoutexception e) {             system.out.println("connection timed out");         } catch (ioexception e) {             throw new runtimeexception(e);         }     } 

is there anyway send 130 56 0 0 2 0 0 0 on wire using java socket writer

say argument sake following necessary

string stringmsg = string.valueof(map, 0, map.length);                  byte[] data = stringmsg.getbytes(); 

a char not meant store binary data. yes, in c, stores signed, 8 bit integral number; in java, however, char utf16 code unit.

you want byte here; more specifically, want bytebuffer:

final bytebuffer buf = bytebuffer.allocate(8);  buf.put((byte) 130); // etc etc  // send buf.array() on wire; or use socketchannel , use buf directly 

and yes, pain have cast byte each time, how :/


also, lot of people confused (i when began java coming c), fact primitive types unsigned not matter @ all; bits remain bits. instance, byte -128 (or byte.min_value) 1111 1111. unless have perform arithmetic operations on numbers, fact primitive negative or not has no influence @ all. , way, why java has 2 operators left byte shifting, >> , >>> (the second 1 not "carry" sign bit).

by way, java primitive types big endian -- if underlying architecture isn't.


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -