Shop OBEX P1 Docs P2 Docs Learn Events
Byte woes — Parallax Forums

Byte woes

AleksAleks Posts: 52
edited 2008-08-07 14:36 in General Discussion
I'm struggling with accessing the individual bytes of the integers used by the Java language. Can someone explain how to access the high and low bytes of an

int x;

?

Comments

  • AleksAleks Posts: 52
    edited 2008-07-18 13:23
    Ok nevermind. I'll leave this thread up here for anyone else wondering about this. The simplest way to handle this is to create two bytes, a high and a low, and then use this routine to separate the bytes from the int and set them as the high and low bytes :

    int x = 375; // this is the word to be separated into a high and low byte. the binary equivalent is 101110111, or high = 1 low = 119
    byte high, low;
    high = 0;
    low = 0;
    while (x > 255)
    {
    x -= 255;
    high++;
    }
    low += x;
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2008-07-18 14:55
    int value16;
    int low = value16 & 0xFF; //extract lower 8 bits
    int high = value16 >>> 8; //extract higher 8 bits

    regards peter
  • AleksAleks Posts: 52
    edited 2008-08-07 14:36
    Many thanks Peter, that definitely simplifies things
Sign In or Register to comment.