binary array to byte type?

Discussion in 'Java' started by djbitchpimp@snowboard.com, Oct 3, 2005.

  1. Guest

    I am trying to figure out a way to do the following.

    I have a int [64] array of 1's and 0's representing a 64-bit number. I
    would like to collapse this into a byte [8] array. Does anyone have any
    ideas on an easy way to accomplish this?
    , Oct 3, 2005
    #1
    1. Advertising

  2. Roedy Green Guest

    On 2 Oct 2005 23:35:05 -0700, wrote or
    quoted :

    >I have a int [64] array of 1's and 0's representing a 64-bit number. I
    >would like to collapse this into a byte [8] array. Does anyone have any
    >ideas on an easy way to accomplish this?


    You need to learn to do bit fiddling. See
    http://mindprod.com/jgloss/binary.html
    and
    http://mindprod.com/jgloss/masking.html
    and chase links.

    The way I would handle it is first create a 64 bit long by shifting
    left and adding another low order bit in a loop.

    When than was done, I would strip off the low order byte, save it, and
    shift right 8 bits in a loop.

    You need to know the >>> and << & | bit operators. See
    http://mindprod.com/jgloss/precedence.html

    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Again taking new Java programming contracts.
    Roedy Green, Oct 3, 2005
    #2
    1. Advertising

  3. Guest

    OK I tried this:

    int byteVal = 0 ;
    int temp = 0 ;
    byteVal = encryptedInt[0] & 0x1 ;
    byteVal <<= 7 ;
    for (i = 7; i > 0; i--) {

    temp = encryptedInt [8 - i] & 0x1 ;
    //System.out.print ("Bit " + (8 - i) + " " + temp + " ") ;
    temp <<= i - 1 ;
    //System.out.println (temp) ;
    byteVal = byteVal | temp ;
    //System.out.println (byteVal) ;
    }

    // byteval has the value of the byte
    byte test1 = 0 ;
    test1 = (byte) test1 | byteVal ;

    But it will not let me cast it back to a byte! How can I get this
    information into a byte type?
    , Oct 3, 2005
    #3
  4. Roedy Green Guest

    On 3 Oct 2005 02:02:32 -0700, wrote or
    quoted :

    > test1 = (byte) test1 | byteVal ;
    >
    >But it will not let me cast it back to a byte!


    Your problem is precedence. just what do you think you are casting?
    when in doubt add ().
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Again taking new Java programming contracts.
    Roedy Green, Oct 3, 2005
    #4
  5. > I have a int [64] array of 1's and 0's representing a 64-bit number. I
    > would like to collapse this into a byte [8] array. Does anyone have any
    > ideas on an easy way to accomplish this?


    see http://uio.imagero.com

    with Unified I/O you could make something like this:

    public byte [] pack(int [] num) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    BitOutputStream bitOut = new BitOutputStream(out);
    bitOut.setBitsToWrite(1);
    RandomAccessRO ro = RandomAccessFactory.createBuffered(num);
    for(int i = 0; i < num.length; i++) {
    int a = ro.read();
    bitOut.write(a);
    }
    bitOut.flush();
    bitOut.close();
    return out.toByteArray();
    }

    --
    Andrey Kuznetsov
    http://uio.imagero.com Unified I/O for Java
    http://reader.imagero.com Java image reader
    http://jgui.imagero.com Java GUI components and utilities
    Andrey Kuznetsov, Oct 3, 2005
    #5
  6. Guest

    How can I import the libraries for BitOutputStream using eclipse?
    , Oct 3, 2005
    #6
  7. <> wrote:
    > OK I tried this:
    >
    > int byteVal = 0 ;

    [...]
    > // byteval has the value of the byte
    > byte test1 = 0 ;
    > test1 = (byte) test1 | byteVal ;

    Because you didn't give parentheses, the compiler interpreted it according
    to the precedence rules, which is:
    test1 = ((byte) test1) | byteVal;
    You want:
    test1 = (byte) (test1 | byteVal);
    >
    > But it will not let me cast it back to a byte! How can I get this
    > information into a byte type?
    >

    --
    "TFritsch$t-online:de".replace(':','.').replace('$','@')
    Thomas Fritsch, Oct 3, 2005
    #7
  8. Andrey Kuznetsov, Oct 3, 2005
    #8
  9. Roedy Green Guest

    On 3 Oct 2005 09:58:10 -0700, wrote or
    quoted :

    >How can I import the libraries for BitOutputStream using eclipse?

    click on properties for your project | Java Build Path | Libraries.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Again taking new Java programming contracts.
    Roedy Green, Oct 4, 2005
    #9
  10. Roedy Green Guest

    On Mon, 3 Oct 2005 19:48:42 +0200, "Andrey Kuznetsov"
    <> wrote or quoted :

    >> How can I import the libraries for BitOutputStream using eclipse?

    >just download uio.jar - http://uio.imagero.com/download/uio.jar


    Questions: I am being bit of a shill here, but you too have
    recognised the chaos in Java's i/o libraries and you too have done
    something concrete to remedy the situation.

    So some questions about imagero Unified IO.

    1. how big is the jar?

    2. how much does it cost?

    3. what are the restrcitions on using it in my own apps?

    4. where can I see the JavaDoc to get a flavour of how it works?

    5. where can I see some sample code?

    6. how fast is it compared with the Sun classes?

    7. Did you do anything to make it more consistent which methods throw
    an IOException?
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Again taking new Java programming contracts.
    Roedy Green, Oct 4, 2005
    #10
  11. > Questions: I am being bit of a shill here, but you too have
    > recognised the chaos in Java's i/o libraries and you too have done
    > something concrete to remedy the situation.
    >
    > So some questions about imagero Unified IO.
    >
    > 1. how big is the jar?

    ~120 kb

    > 2. how much does it cost?

    nothing

    > 3. what are the restrcitions on using it in my own apps?

    it is under BSD

    > 4. where can I see the JavaDoc to get a flavour of how it works?

    http://uio.imagero.com/doc/

    > 5. where can I see some sample code?

    Sorry, I didn't published any examples jet. Just ask me.

    > 6. how fast is it compared with the Sun classes?

    it depends on how you use sun classes.
    In worst case (repeat readInt()/writeInt()) you have 100/120 times speed
    gain.
    See http://uio.imagero.com/performance.html

    > 7. Did you do anything to make it more consistent which methods throw
    > an IOException?

    please elaborate

    BTW uio includes also many utilities like MemoryManager and OpenFileManager.

    --
    Andrey Kuznetsov
    http://uio.imagero.com Unified I/O for Java
    http://reader.imagero.com Java image reader
    http://jgui.imagero.com Java GUI components and utilities
    Andrey Kuznetsov, Oct 4, 2005
    #11
  12. Roedy Green Guest

    On Tue, 4 Oct 2005 17:16:11 +0200, "Andrey Kuznetsov"
    <> wrote or quoted :

    >Sorry, I didn't published any examples jet. Just ask me.


    This is probably what you need to get it to catch on. The learning
    curve is too steep otherwise, no matter how simple it actually is.

    Sun has little tutorials for all the Swing components. When you read
    them first, the JavaDoc suddenly comes to life and seems 100 times
    simpler.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Again taking new Java programming contracts.
    Roedy Green, Oct 4, 2005
    #12
  13. Roedy Green Guest

    On Tue, 4 Oct 2005 17:16:11 +0200, "Andrey Kuznetsov"
    <> wrote or quoted :

    >> 7. Did you do anything to make it more consistent which methods throw
    >> an IOException?

    >please elaborate

    It is just that read/close seemingly randomly either throw or do not
    throw an IOException. IT is difficult to memorise the pattern. I
    figured you might do your class so they always do, even if they in
    practice never throw it. That leaves the door open to in future
    without changing client code.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Again taking new Java programming contracts.
    Roedy Green, Oct 4, 2005
    #13
  14. Roedy Green Guest

    On Tue, 4 Oct 2005 17:16:11 +0200, "Andrey Kuznetsov"
    <> wrote or quoted :

    >See http://uio.imagero.com/performance.html


    This page is very confusing. I suggest you redo it as a table, and
    perhaps normalize the figures, with an note that either bigger or
    smaller is better.

    If you have the patience, present the benchmarks in graph form for
    more punch.

    Also try to give each benchmark a name that does not require you to
    know uio to know what it signifies.

    I have updated the uio entry at
    http://mindprod.com/jgloss/unfiedio.html
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Again taking new Java programming contracts.
    Roedy Green, Oct 4, 2005
    #14
  15. > This is probably what you need to get it to catch on. The learning
    > curve is too steep otherwise, no matter how simple it actually is.


    Interface of uio is rougly same as of RandomAccessFile.

    Important thing is RandomAccessFactory.
    RandomAccessFactory has 6 methods:

    RandomAccess create()
    RandomAccessRO createRO()

    RandomAccessBuffer createBuffered()
    RandomAccessBufferRO createBufferedRO()

    RandomAccessByteArray create()
    RandomAccessByteArrayRO createRO()

    You can also read from/write to primitive arrays.
    readFully(int [] a, int offset, int length)
    write(double [] d, int offset, int length)

    --
    Andrey Kuznetsov
    http://uio.imagero.com Unified I/O for Java
    http://reader.imagero.com Java image reader
    http://jgui.imagero.com Java GUI components and utilities
    Andrey Kuznetsov, Oct 4, 2005
    #15
  16. >>> 7. Did you do anything to make it more consistent which methods throw
    >>> an IOException?

    >>please elaborate

    > It is just that read/close seemingly randomly either throw or do not
    > throw an IOException. IT is difficult to memorise the pattern. I
    > figured you might do your class so they always do, even if they in
    > practice never throw it. That leaves the door open to in future
    > without changing client code.


    IOutils contains method closeStream() which
    a) does not throws IOException
    b) does not throws NullPointerException - i.e. given Stream may be null.

    read always declared "throws IOException"

    --
    Andrey Kuznetsov
    http://uio.imagero.com Unified I/O for Java
    http://reader.imagero.com Java image reader
    http://jgui.imagero.com Java GUI components and utilities
    Andrey Kuznetsov, Oct 4, 2005
    #16
  17. > This page is very confusing. I suggest you redo it as a table, and
    > perhaps normalize the figures, with an note that either bigger or
    > smaller is better.
    >
    > If you have the patience, present the benchmarks in graph form for
    > more punch.
    >
    > Also try to give each benchmark a name that does not require you to
    > know uio to know what it signifies.


    if I have time someday...

    > I have updated the uio entry at
    > http://mindprod.com/jgloss/unfiedio.html


    thanks
    http://mindprod.com/jgloss/unifiedio.html

    --
    Andrey Kuznetsov
    http://uio.imagero.com Unified I/O for Java
    http://reader.imagero.com Java image reader
    http://jgui.imagero.com Java GUI components and utilities
    Andrey Kuznetsov, Oct 4, 2005
    #17
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bharat Bhushan

    Appending byte[] to another byte[] array

    Bharat Bhushan, Aug 5, 2003, in forum: Java
    Replies:
    15
    Views:
    40,234
    Roedy Green
    Aug 5, 2003
  2. Kirby
    Replies:
    3
    Views:
    637
    Kirby
    Oct 8, 2004
  3. Guest
    Replies:
    2
    Views:
    10,901
    John Harrison
    Jul 26, 2004
  4. Replies:
    20
    Views:
    9,746
    licebmi
    Sep 7, 2009
  5. Tom McGlynn
    Replies:
    4
    Views:
    851
    Mark Space
    Apr 19, 2008
Loading...

Share This Page