Velocity Reviews > Java > binary array to byte type?

# binary array to byte type?

djbitchpimp@snowboard.com
Guest
Posts: n/a

 10-03-2005
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?

Roedy Green
Guest
Posts: n/a

 10-03-2005
On 2 Oct 2005 23:35:05 -0700, http://www.velocityreviews.com/forums/(E-Mail Removed) 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

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

--
http://mindprod.com Again taking new Java programming contracts.

djbitchpimp@snowboard.com
Guest
Posts: n/a

 10-03-2005
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?

Roedy Green
Guest
Posts: n/a

 10-03-2005
On 3 Oct 2005 02:02:32 -0700, (E-Mail Removed) 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?
--
http://mindprod.com Again taking new Java programming contracts.

Andrey Kuznetsov
Guest
Posts: n/a

 10-03-2005
> 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++) {
bitOut.write(a);
}
bitOut.flush();
bitOut.close();
return out.toByteArray();
}

--
Andrey Kuznetsov
http://uio.imagero.com Unified I/O for Java
http://jgui.imagero.com Java GUI components and utilities

djbitchpimp@snowboard.com
Guest
Posts: n/a

 10-03-2005
How can I import the libraries for BitOutputStream using eclipse?

Thomas Fritsch
Guest
Posts: n/a

 10-03-2005
<(E-Mail Removed)> 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('\$','@')

Andrey Kuznetsov
Guest
Posts: n/a

 10-03-2005
> How can I import the libraries for BitOutputStream using eclipse?
and include it in classpath (or whatever you should do with eclipse - I
never used it - cos I have IntelliJ IDEA).

--
Andrey Kuznetsov
http://uio.imagero.com Unified I/O for Java
http://jgui.imagero.com Java GUI components and utilities

Roedy Green
Guest
Posts: n/a

 10-04-2005
On 3 Oct 2005 09:58:10 -0700, (E-Mail Removed) wrote or
quoted :

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

click on properties for your project | Java Build Path | Libraries.
--
http://mindprod.com Again taking new Java programming contracts.

Roedy Green
Guest
Posts: n/a

 10-04-2005
On Mon, 3 Oct 2005 19:48:42 +0200, "Andrey Kuznetsov"
<(E-Mail Removed)> wrote or quoted :

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

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?
--
http://mindprod.com Again taking new Java programming contracts.

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is OffTrackbacks are On Pingbacks are On Refbacks are Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post jeff@foundrymusic.com C++ 20 09-07-2009 08:54 PM Tom McGlynn Java 4 04-18-2008 11:49 PM Kirby Java 3 10-08-2004 03:01 AM C++ 2 07-26-2004 08:08 PM Bharat Bhushan Java 15 08-05-2003 07:52 PM