Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

bytes in Java

  • 11-02-2004 12:30AM
    #1
    Closed Accounts Posts: 999 ✭✭✭


    I'm trying to create a 64k temporary memory in Java. Now this should be just a byte array like so,
    byte[] mem = new byte[65536];
    That works fine.
    Now my problem is I wan't to store values in the range of 0 to 255 in these bytes. this should be fine as a byte value 1111 1111 give an int value of 255. Theoretically!
    Unfortunately because of the leading 1 Java interprets this as a negative and returns -128. So I have a range of -128 to 127. This is causing me no end of problems. Here's some of what I tried so far,
    [PHP]
    if(value > 127)
    {
    value = value - 127;
    value = 0 - value;
    }
    String s = Integer.toString(value);

    byte b = Byte.parseByte(s);

    memArray = b;
    [/PHP]
    That all compiles fine but when I get to a later point in the program,
    [PHP]
    memInt = memArray[p].intValue();
    [/PHP]
    I get this compilation error,

    byte cannot be dereferenced
    memInt = memArray[p].intValue();

    Where 'p' is an integer value. I've tried a lot of things so far and this is the closest I've come to a clean compile so let me know what I'm not getting here. Is anything being loaded into the 'memArray' bytes? And if there is then why can't I get at them?


Comments

  • Closed Accounts Posts: 1,322 ✭✭✭phobos


    I'm sure there has to be a better way, and I know I'll probably receive slaps later, but this should sort you out (for now).
    public int getInt(byte b){
    		
            int value = new Byte(b).intValue();
    		
            if(value < 0)
                    value += 256;	
    		
            return value;
    }
    

    HTH ;)

    ;-phobos-)


  • Closed Accounts Posts: 999 ✭✭✭Raz


    Well that cleared up my dereferencing error. Thanks phobos. I still have problems but at least I can now get it to compile :)


  • Registered Users, Registered Users 2 Posts: 491 ✭✭Silent Bob


    Mask it so that when you cast to int the sign bit isn't treated as a sign bit
    public int byte_as_int(byte b)
    {
        return (int) (b & 0xff);
    }
    

    Also the reason for your dereference error is that you tried to treat a byte primitive as an object when you used memArray[p].intValue();


  • Closed Accounts Posts: 999 ✭✭✭Raz


    Ahhhh! It makes more sense now. I was using Byte objects and then I switched to byte primitives.
    I've yet to implement the bit of code but thanks bob.


  • Closed Accounts Posts: 999 ✭✭✭Raz


    Bob, your advice has helped me straighten out all my problems with that class.
    I'm very grateful for your help, the sign bit thing was one hell of a headache! Thanks.


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 491 ✭✭Silent Bob


    Glad to be of use :)


Advertisement