0

I want to built a UDP based Protocol which is able to send a file from a client to an server. The restriction is that the data which is send is exactly <x> bytes big. For example one packages should contain a 32-bit sessionnumber, a 8-bit packagenumber and a 10-byte large word in ASCII-Code.

I know that I have to initialize a byte-array which is in this case 15 bytes long but how can I write into it that I have the form I need?

byte[] array = new byte[15];

I tried to write "starting" in ASCII in the byte-array:

array[6] = 115;
array[7] = 116;
array[8] = 97;
array[9] = 114;
array[10] = 116;
array[11] = 105;
array[12] = 110;
array[13] = 103;

But it's not working how it should. Please give an explanation with your answer because I want to understand and learn with it.

Mr Lister
  • 45,515
  • 15
  • 108
  • 150
missjohanna
  • 293
  • 1
  • 2
  • 15
  • what do you mean by, **But its not working**? – Sabir Khan Jan 04 '16 at 13:50
  • Sorry I explained it a bit wrong. I meant the other things aren't working. For example how can I set a 32-bit large number in this array. I will give you a pseudo code: array[0-1] = <32-bit number>; – missjohanna Jan 04 '16 at 18:44
  • what you are asking is not very straightforward to achieve if you are not willing to use separate `int` variable for that . Refer these questions to know more , [Convert integer into byte array (Java)](http://stackoverflow.com/questions/1936857/convert-integer-into-byte-array-java) , [Convert the bytes of byte array to bits and store in Integer array](http://stackoverflow.com/questions/21127514/convert-the-bytes-of-byte-array-to-bits-and-store-in-integer-array) and [Convert](http://stackoverflow.com/questions/13203426/convert-4-bytes-to-an-unsigned-32-bit-integer-and-storing-it-in-a-long) – Sabir Khan Jan 05 '16 at 04:10
  • I believe ByteBuffer would be the besteht solution for me. Then I can built it easy. Anyway THX for your help – missjohanna Jan 06 '16 at 09:52

0 Answers0