Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Int to Bits conversion

MeldrozMeldroz Posts: 38Member
Grettings,

i have an intetger var, and i'm trying to get specific bytes from them.

ex :

int foo= 67;

byte[4] b = new byte[4]; // int has 4 bytes in java

b = foo | 1 << (7-8); // should give me the first 8 bits of foo but not working, probably doing it wrong.

i'm basically trying to extract the bits 0-7, 8-15, and so on and store them in my array of byte.

Any pointers would help, i am weak when it comes to bit manipulation.
Thank you,

Mel








Comments

  • arb123arb123 Posts: 696Member
    The toBytes method below should do it:

    [code]
    public class IntToBytes {

    public static byte[] toBytes(int n) {
    byte[] barr = new byte[4];

    for (int i = barr.length - 1; i >= 0; i--) {
    barr[i] = (byte) n;
    n >>= 8;
    }

    return barr;
    }

    public static void println(byte[] barr) {
    for (int i = 0; i < barr.length; i++) {
    print(barr[i]);
    }
    System.out.println();
    }

    public static void print(byte b) {
    int mask = 0x80;
    while (mask > 0) {
    if ((mask & b) != 0) {
    System.out.print('1');
    } else {
    System.out.print('0');
    }
    mask >>= 1;
    }
    }

    public static void main(String[] args) {
    int[] tests = { 0x00000001, 0xFFFFFFFF, 0x80000000, };

    for (int i = 0; i < tests.length; i++) {
    System.out.print(tests[i] + " = ");
    byte[] barr = toBytes(tests[i]);
    println(barr);
    }
    }
    }
    [/code]

    ---------------------------------
    [size=1]HOWTO ask questions: http://catb.org/~esr/faqs/smart-questions.html[/size]

  • MeldrozMeldroz Posts: 38Member
    Thanks for the help, yeah i was trying to do something like this, as part of a bigger program.

    thanks again,
    Mel

    : The toBytes method below should do it:
    :
    : [code]
    : public class IntToBytes {
    :
    : public static byte[] toBytes(int n) {
    : byte[] barr = new byte[4];
    :
    : for (int i = barr.length - 1; i >= 0; i--) {
    : barr[i] = (byte) n;
    : n >>= 8;
    : }
    :
    : return barr;
    : }
    :
    : public static void println(byte[] barr) {
    : for (int i = 0; i < barr.length; i++) {
    : print(barr[i]);
    : }
    : System.out.println();
    : }
    :
    : public static void print(byte b) {
    : int mask = 0x80;
    : while (mask > 0) {
    : if ((mask & b) != 0) {
    : System.out.print('1');
    : } else {
    : System.out.print('0');
    : }
    : mask >>= 1;
    : }
    : }
    :
    : public static void main(String[] args) {
    : int[] tests = { 0x00000001, 0xFFFFFFFF, 0x80000000, };
    :
    : for (int i = 0; i < tests.length; i++) {
    : System.out.print(tests[i] + " = ");
    : byte[] barr = toBytes(tests[i]);
    : println(barr);
    : }
    : }
    : }
    : [/code]
    :
    : ---------------------------------
    : [size=1]HOWTO ask questions: http://catb.org/~esr/faqs/smart-questions.html[/size]
    :
    :

Sign In or Register to comment.