469,366 Members | 2,236 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,366 developers. It's quick & easy.

Appending byte[] to another byte[] array

Hi,

I need to generate random bytes for x number of times and keep appending it
to a bigger byte[] array. How can I do this ?

for (int lctr=0; lctr < main.Main.NoOfattributes(); lctr=lctr+1){

// This line below generates the random bits for a given length
byte[] intervals = RandomFunctions.makeRandomGeneBits(length);

biggerByteArray[] = biggerByteArray + intervals ; // this is the
idea...obviously this statement doesn't work for byte[]
}

Any help, pointers on web are appreciated.
Many Thanks,

- Bharat.
Jul 17 '05 #1
4 58963

"Bharat Bhushan" <bh*****@talk21.com> wrote in message
news:xl****************@news-binary.blueyonder.co.uk...
Hi,

I need to generate random bytes for x number of times and keep appending it to a bigger byte[] array. How can I do this ?

for (int lctr=0; lctr < main.Main.NoOfattributes(); lctr=lctr+1){

// This line below generates the random bits for a given length
byte[] intervals = RandomFunctions.makeRandomGeneBits(length);

biggerByteArray[] = biggerByteArray + intervals ; // this is the
idea...obviously this statement doesn't work for byte[]
}

Any help, pointers on web are appreciated.
Many Thanks,

- Bharat.


byte[] intervals = ...

byte[] biggerByteArray = ...

byte[] temp = new byte[intervals.length + biggerByteArray.length];

for(int i=0; i< intervals.length; i++) temp[i] = intervals[i];
for(int i=0; i< biggerByteArray.length; i++) temp[i + intervals.length] =
biggerByteArray[i];

biggerByteArray = temp;

...but if you are doing this lots of times it is desperately inefficient.
Much better to copy into a byte[][] and then "flatten" to byte[] once you
know the size.

(i didnt bother compiling this so it may not work).

John.

Jul 17 '05 #2
Mr. J M Court wrote:
"Bharat Bhushan" <bh*****@talk21.com> wrote in message
news:xl****************@news-binary.blueyonder.co.uk...
Hi,

I need to generate random bytes for x number of times and keep appending
it
to a bigger byte[] array. How can I do this ?

for (int lctr=0; lctr < main.Main.NoOfattributes(); lctr=lctr+1){

// This line below generates the random bits for a given length
byte[] intervals = RandomFunctions.makeRandomGeneBits(length);

biggerByteArray[] = biggerByteArray + intervals ; // this is the
idea...obviously this statement doesn't work for byte[]
}

Any help, pointers on web are appreciated.
Many Thanks,

- Bharat.

byte[] intervals = ...

byte[] biggerByteArray = ...

byte[] temp = new byte[intervals.length + biggerByteArray.length];


It would be better to use a System.arrayCopy for each of the arrays here:

System.arraycopy(intervals,0,temp,0, intervals.length);
System.arraycopy(biggerByteArray,0,temp,intervals. length,
biggerByteArray.length);

biggerByteArray = temp;

The array copy does the same job as the for loops suggested, but is more
effecient.

Cheers

Rory

for(int i=0; i< intervals.length; i++) temp[i] = intervals[i];
for(int i=0; i< biggerByteArray.length; i++) temp[i + intervals.length] =
biggerByteArray[i];

biggerByteArray = temp;

..but if you are doing this lots of times it is desperately inefficient.
Much better to copy into a byte[][] and then "flatten" to byte[] once you
know the size.

(i didnt bother compiling this so it may not work).

John.


Jul 17 '05 #3
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Bharat Bhushan wrote:
Hi,

I need to generate random bytes for x number of times and keep
appending it to a bigger byte[] array. How can I do this ?

[snip]

Hi,
I don't know about efficiency (John suggested using a byte[][] and
"flattening" it at the end, which is probably fairly efficient), but
for convenience, you could just create a ByteArrayOutputStream, then
write each byte[] into it and convert it using toByteArray() at the
end. You have to catch IOExceptions, but, AFAIK, they're pretty much
"can't happen" with ByteArrayOutputStream.
- --
Chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.2 (GNU/Linux)

iD8DBQE/L4H4wxczzJRavJYRAlACAJ4wppAjjtmnyRkCRlxx2Is14uFWiA CgoBui
JguxSEhknuaCjazr4puzxEo=
=y8WP
-----END PGP SIGNATURE-----
Jul 17 '05 #4
On Tue, 5 Aug 2003 12:15:53 +0100, "Bharat Bhushan"
<bh*****@talk21.com> wrote or quoted :
I need to generate random bytes for x number of times and keep appending it
to a bigger byte[] array. How can I do this ?


see http://mindprod.com/jgloss/random.html

you can generate them a byte or int or long at a time and fill in your
array.

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
Jul 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

15 posts views Thread by jeko | last post: by
2 posts views Thread by Tim Conner | last post: by
1 post views Thread by Wasim Akram | last post: by
32 posts views Thread by Guoqi Zheng | last post: by
6 posts views Thread by Narshe | last post: by
6 posts views Thread by Seabass | last post: by
6 posts views Thread by Ashit Vora | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.