byte arrays

Discussion in 'Java' started by cryptogirl, Feb 25, 2006.

  1. cryptogirl

    cryptogirl Guest

    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
    cryptogirl, Feb 25, 2006
    #1
    1. Advertising

  2. cryptogirl

    VisionSet Guest

    "cryptogirl" <> wrote in message
    news:...
    > 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

    --
    Mike W
    VisionSet, Feb 25, 2006
    #2
    1. Advertising

  3. cryptogirl

    Paul Hamaker Guest

    Paul Hamaker, Feb 25, 2006
    #3
  4. cryptogirl

    Paul Hamaker Guest

    Oops, correction : its range is -128 thru 127, sorry.
    Paul Hamaker, Feb 25, 2006
    #4
  5. cryptogirl

    VisionSet Guest

    "cryptogirl" <> wrote in message
    news:...

    > 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!

    --
    Mike W
    VisionSet, Feb 25, 2006
    #5
  6. cryptogirl

    cryptogirl Guest

    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?
    cryptogirl, Feb 25, 2006
    #6
  7. cryptogirl

    Roedy Green Guest

    On 25 Feb 2006 12:41:04 -0800, "cryptogirl" <>
    wrote, quoted or indirectly quoted someone who said :

    >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.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
    Roedy Green, Feb 25, 2006
    #7
  8. cryptogirl

    Roedy Green Guest

    On 25 Feb 2006 14:00:21 -0800, "cryptogirl" <>
    wrote, quoted or indirectly quoted someone who said :

    >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?


    You want an UNSIGNED byte. You have to fake it. see
    http://mindprod.com/jgloss/unsigned.html
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
    Roedy Green, Feb 25, 2006
    #8
  9. cryptogirl

    Filip Larsen Guest

    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,
    --
    Filip Larsen
    Filip Larsen, Feb 26, 2006
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andreas
    Replies:
    1
    Views:
    843
    Jonathan Bromley
    May 4, 2004
  2. Bharat Bhushan

    Appending byte[] to another byte[] array

    Bharat Bhushan, Aug 5, 2003, in forum: Java
    Replies:
    15
    Views:
    40,234
    Roedy Green
    Aug 5, 2003
  3. Jean-Daniel Gamache
    Replies:
    0
    Views:
    419
    Jean-Daniel Gamache
    Jul 14, 2004
  4. Peter
    Replies:
    3
    Views:
    724
    Michael Borgwardt
    Aug 5, 2004
  5. Philipp
    Replies:
    21
    Views:
    1,123
    Philipp
    Jan 20, 2009
Loading...

Share This Page