Kimball Larsen wrote:
Ok, so I've got a degree in CS and all, but it's been since I earned that degree that I've ever had to mess with stuff like this. I am working on someone else's code (this someone is no longer at my disposal) and have the following method:


    public static int get2Little(byte abyte0[], int i) throws Exception
    {
        return abyte0[i + 1] << 8 & 0xff00 | abyte0[i + 0] & 0xff;
    }

Hmm...

First, if you like to memorize stuff, you can remember which gets evaluated first, "<<" or "&". Most of us have better things to remember. So I'd sprinkle parentheses all over to clarify.

Second, "abyte0[i + 1]" is a byte, so "abyte0[i + 1] << 8" is also a byte. But you've shifted all its data out, giving you 0. You need a cast.

Maybe this is what you're after:

    public static int get2Little(byte abyte0[], int i)
    {
        return (((int) abyte0[i + 1]) << 8) | abyte0[i];
    }

That might work, unless it trips over an issue with signs. :-)

Shane

/*
PLUG: http://plug.org, #utah on irc.freenode.net
Unsubscribe: http://plug.org/mailman/options/plug
Don't fear the penguin.
*/

Reply via email to