• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 463
  • Last Modified:

get the 6 first bytes of a byte array

Hi,

I have a byte array, I would like to retrieve the 6 first bytes of this byte array.
And then, in certain cases, I need to remove those bytes from the array.
0
matthew016
Asked:
matthew016
  • 3
  • 2
  • 2
  • +1
3 Solutions
 
objectsCommented:
you'll need to use arraycopy for that
for example to get the first 6 bytes create a new array of 6 bytes and copy 6 bytes from original array into it

there are some examples here
http://helpdesk.objects.com.au/?s=arraycopy
0
 
VenabiliCommented:
and to remove these 6 bytes, just create a new array with length originalLength-6 and copy the remaining ones there.

No easy removing of elements with arrays I am afraid :)
0
 
CEHJCommented:
You could use nio:
	ByteBuffer bb = ByteBuffer.wrap(yourByteArray);
	bb.limit(6);

	byte[] start = new byte[6];
	bb.get(start);
	System.out.println(java.util.Arrays.toString(start));

	if (true) { // replace by your condition
	    bb.limit(yourByteArray.length);
	    byte[] end = new byte[yourByteArray.length - 6];
	    bb.get(end);
	    System.out.println(java.util.Arrays.toString(end));
	}

Open in new window

0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
matthew016Author Commented:
Hi,

I tried but can't make it to work, see my code below :

Actually I have a ByteBuffer :

java.util.Arrays.toString(  this.readBuffer.array()   )

contains : [49, 59, 48, 59, 59, 59, 59, 59, 59, 59, 117, 115, 101, 114, 53, ....

Then I try :

byte[] bytesLentgh = new byte[6];
this.readBuffer.get(bytesLentgh);
System.out.println(java.util.Arrays.toString(bytesLentgh));

The code above will always display :
[0, 0, 0, 0, 0, 0]
0
 
objectsCommented:
you should be setting position first

byte[] bytesLentgh = new byte[6];
this.readBuffer.pos(0);  // assuming you want the first 6 bytes
this.readBuffer.get(bytesLentgh);
System.out.println(java.util.Arrays.toString(bytesLentgh));
0
 
matthew016Author Commented:
Thank you,

Then, how do I remove those 6 bytes from the ByteBuffer ?
0
 
objectsCommented:
readBuffer.compact();  // (if supported by the implementation you are using)
0
 
CEHJCommented:
:-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now