byte arrays

C

cryptogirl

Hello,

I have a question concerning byte arrays. I'm trying to initialize a
byte array from 1 to 255 by doing the following:
byte S[] = new byte [255];
for(int i=0; i<256;; i++){
S=i;

Except that every time it reaches 127 it becomes -127 and I get an
index out of bounds error. Any idea how to fix this?

Thank you
 
V

VisionSet

cryptogirl said:
Hello,

I have a question concerning byte arrays. I'm trying to initialize a
byte array from 1 to 255 by doing the following:
byte S[] = new byte [255];
for(int i=0; i<256;; i++){
S=i;

Except that every time it reaches 127 it becomes -127 and I get an
index out of bounds error. Any idea how to fix this?


A byte in Java is signed so max value is 127 not 255
but that isn't your problem since the index is an int and that does not flip
to -127.
But you do initialise your array with a size of 255 and you want 256
ie 0 through to 255 is 256 pockets in your array.
I take it your two ;; is a typo
 
V

VisionSet

byte S[] = new byte [255];

in Java we start variable names with lowercase
only classes get uppercase

ie

class MyClass
int myInt
void myMethod()

so it should be:

byte s[] = new byte[256];

conventions are good!
 
C

cryptogirl

yes I found some of my errors.. I just want to know since I'm trying to
generate a byte key stream.. and i would like it to be of type byte,
rather then int. I've had it has type int before hand. Am i better off
with type int?
 
R

Roedy Green

byte S[] = new byte [255];
for(int i=0; i<256;; i++){
S=i;


this should not even compile.

you have two semicolons after your 256. there should be only one.

S is a the name of a class, not an array. It should be s[].
s= i;

i in an int you need a byte, so you must cast it to byte before
storing.

The problem you described is for quite different code. Always
copy/paste the troublesome code so you don't inadvertently fix or
introduce new errors.
 
F

Filip Larsen

cryptogirl wrote
I have a question concerning byte arrays. I'm trying to initialize a
byte array from 1 to 255 by doing the following:
byte S[] = new byte [255];
for(int i=0; i<256;; i++){
S=i;

Except that every time it reaches 127 it becomes -127 and I get an
index out of bounds error. Any idea how to fix this?


Take a look at the working code below. Besides the errors in your code
that people already have commented on, you should note that it is
possible to use byte variables to store unsigned values. The trick to
remember is to mask off the sign extension bits when the byte
implicitely is extended into a signed int by the compiler, as is done
with "s & 0xFF" below.


public class ByteArrayDemo {

public static void main(String[] args) {
byte[] s = new byte[256];
// store 0 through 255 into s
for (int i = 0; i < s.length; i++) {
s = (byte) i;
}
// retrieve the values back
for (int i = 0; i < s.length; i++) {
int value = s & 0xFF;
System.out.println("s["+i+"] = " + value);
}
}

}


Regards,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top