473,325 Members | 2,308 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

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 59578

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: jeko | last post by:
Hi, is it possible to read a double variable one byte at a time. I would want store each byte into unsigned char and then assemble again the double variable again. thanx Andrea
2
by: Tim Conner | last post by:
Which functions are available to move a part from byte into another byte ? Let's say I have a byte of 20 and another of 40. And I want to move 5th to 15th from the first byte into occupping bytes...
1
by: Wasim Akram | last post by:
Hi, I have a field "Month" in my SQL server table. The type of this field is "tinyint". Now what I am doing in the code is using DataRow to read this field in a 'int' variable. int month...
5
by: Eric | last post by:
Is there a class that will allow me to append a byte to another? The data that I have is binary so StringBuilder will not work.
32
by: Guoqi Zheng | last post by:
I am really do not know so much about byte/bit, etc. Question, if I defined a byte(), how can I add a single byte to it? what I want is that I have an array of bytes, I loop that array, look at...
6
by: Narshe | last post by:
How can I convert a byte into a byte?? You can assign a byte to a byte?, but you obviously can't do that with an array. The way I'm currently doing it is a for loop that just copies the data...
6
by: Seabass | last post by:
Hello everyone, I'm trying to do a Hex Viewer in C#. I'm reading the user selected file byte by byte but it seems to take forever ( one minute to read a larger file ). Is there any way to make...
3
by: efdeugenio | last post by:
Hi, I will really appreciate if someone cans help me with this: I have a managed c++ class that I am calling from C#. The declaration of a function in this class is: bool CanAddTemplate(unsigned...
6
by: Ashit Vora | last post by:
Hi, I had one small query. I 'm coping data from an unsigned short to a char*. code is #include<stdio.h> int main(void){ unsigned short type=0xFE10;
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.