Results 1 to 4 of 4
  1. #1
    gonzalioz is offline Member
    Join Date
    May 2011
    Posts
    8
    Rep Power
    0

    Default Combining multiple ints to 2 bytes.

    Hi all,

    I am trying to convert multiple ints to their binary values and that into a byte.

    int version = 2; // (max 3) binary: 10
    int padding = 0; // (max 1) binary: 0
    int extension = 0; // (max 1) binary: 0
    int cc = 7; // (max 15) binary: 0111

    Together these ints would be: 1000 0111 And I want to convert that to a byte.
    I have no clue how to do this.

    I hope someone can help me, thanks in advance.

    edit: I said 2 bytes in the title sorry... Should be 1 byte of course.
    Last edited by gonzalioz; 05-12-2011 at 01:31 PM. Reason: Wrong title, should be 1 byte.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,512
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by gonzalioz View Post
    Hi all,

    I am trying to convert multiple ints to their binary values and that into a byte.

    int version = 2; // (max 3) binary: 10
    int padding = 0; // (max 1) binary: 0
    int extension = 0; // (max 1) binary: 0
    int cc = 7; // (max 15) binary: 0111

    Together these ints would be: 1000 0111 And I want to convert that to a byte.
    I have no clue how to do this.

    I hope someone can help me, thanks in advance.

    edit: I said 2 bytes in the title sorry... Should be 1 byte of course.
    Have a look at the bit shift left operator << and the inclusive or operator |

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    gonzalioz is offline Member
    Join Date
    May 2011
    Posts
    8
    Rep Power
    0

    Default

    Ah thank you very much! I got a lot further:

    int val = version << 6 | padding << 5 | extension << 4 | cc << 0;
    System.out.println(Integer.toBinaryString(val));

    It now prints the correct binary string. But the result is still an integer. Can you help me to convert val to a byte or convert the binary string to a byte? I couldn't find a java method that is something like Convert.toByte(val) like in .net.

    Thanks!

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,512
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by gonzalioz View Post
    Ah thank you very much! I got a lot further:

    int val = version << 6 | padding << 5 | extension << 4 | cc << 0;
    System.out.println(Integer.toBinaryString(val));

    It now prints the correct binary string. But the result is still an integer. Can you help me to convert val to a byte or convert the binary string to a byte? I couldn't find a java method that is something like Convert.toByte(val) like in .net.

    Thanks!
    Use a type cast like this:

    Java Code:
    byte val= (byte)(version << 6 | padding << 5 | extension << 4 | cc << 0);
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Sum of odd ints 0-n
    By falkon114 in forum New To Java
    Replies: 7
    Last Post: 12-06-2010, 08:36 AM
  2. Replies: 8
    Last Post: 09-24-2010, 03:34 AM
  3. Help with ints
    By Insomniac Riot in forum New To Java
    Replies: 5
    Last Post: 04-02-2010, 03:53 PM
  4. Combining these 2 projects
    By fresh83 in forum New To Java
    Replies: 14
    Last Post: 12-28-2009, 08:52 AM
  5. Help combining loops into 1 program.
    By kewlgeye in forum New To Java
    Replies: 5
    Last Post: 04-22-2008, 09:58 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •