Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

bytes in Java

  • 10-02-2004 11:30pm
    #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