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.

4
  • what do you mean by, But its not working? Commented Jan 4, 2016 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>; Commented Jan 4, 2016 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) , Convert the bytes of byte array to bits and store in Integer array and Convert Commented Jan 5, 2016 at 4:10
  • I believe ByteBuffer would be the besteht solution for me. Then I can built it easy. Anyway THX for your help Commented Jan 6, 2016 at 9:52

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.