0

i do have an byte array included a range of numbers...

t Block and not the rest!

How can i have all block 4-8 in Temp[] ??

5
  • You mean you have 4 bytes which together make up an int? Commented Dec 18, 2012 at 9:20
  • yes i have 4Byte and in Temp also i need to have 4 Bytes but the method i used it only return me the 1th byte and skip the rest how can i return all 4 bytes in my Temp? Commented Dec 18, 2012 at 9:22
  • CMD suppose to be [493131] but now its only 49 the first Byte Commented Dec 18, 2012 at 9:23
  • which byte is the most significant the first or the last? Commented Dec 18, 2012 at 9:29
  • BlockCopy is Correct as i test already the only thing is this Block copy have to read 3 Bytes and store them in one Array Called Temp[] Commented Dec 18, 2012 at 9:39

1 Answer 1

2

Elements 4-8 (or in reality index 3-7) is 5 bytes. Not 4.
You have the source offset and count mixed up:

Buffer.BlockCopy(bResponse, 3, temp, 0, 5);

Now temp will contain [23232].

If you want the last 4 bytes then use this:

Buffer.BlockCopy(bResponse, 4, temp, 0, 4);

Now temp will contain [3232].
To convert this to an int:

if (BitConverter.IsLittleEndian)
  Array.Reverse(temp);

int i = BitConverter.ToInt32(temp, 0);

Edit: (After your comment that [43323232] actually is {43, 32, 32, 32})

var firstByte = temp[0];   // This is 43
var secondByte = temp[1];  // This is 32
var thirdByte = temp[2];   // 32
var fourthByte = temp[3];  // 32

If you want to convert this to an int then the BitConverter example above still works.

Sign up to request clarification or add additional context in comments.

4 Comments

This is not the Answer! This one its only to take 3 block from buffer how you want to assign this 3 in one Temp[]?
Each of this have to read individually and sort in Temp back to back with your method is nothing but only reading 4bytes. I have no problem for reading the problem is assigning this 4 in another Array i call Temp[]
@Artinos: You are wrong about the number of bytes read. If you read the MSDN page of Buffer.BlockCopy you'll see that it is you who have mixed up the source offset and count parameters.
You are Right about the Number My bad in Typing ...However i still cannot Put put the 3 byte block into one block called Temp[]

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.