aspose file tools*
The moose likes Beginning Java and the fly likes Byte Packing/Unpacking Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Byte Packing/Unpacking" Watch "Byte Packing/Unpacking" New topic
Author

Byte Packing/Unpacking

John Carr
Greenhorn

Joined: Jan 16, 2010
Posts: 17
Ok, so I've been trying to figure out bitwise operators and such, and have gone through examples and tutorials, and I can't seem to grasp it because none of them seem to explain what their goal is, so I've come up with a hypothetical, and would appreciate it if someone could show me an example

Say I have 8 values that are true or false and I want to store these in a single byte, send it across a network. Then, unpack it and use it to toggle 8 different things. (I don't need help the network part, just the packing and unpacking)

For example you have:
byte[] vals = { 1, 0 , 1, 0, 1, 0, 1, 1 };

How would you pack vals into a single byte, then unpack it and get a byte array?

Thanks in advance.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19004
    
  40

John Carr wrote:Ok, so I've been trying to figure out bitwise operators and such, and have gone through examples and tutorials, and I can't seem to grasp it because none of them seem to explain what their goal is, so I've come up with a hypothetical, and would appreciate it if someone could show me an example

Say I have 8 values that are true or false and I want to store these in a single byte, send it across a network. Then, unpack it and use it to toggle 8 different things. (I don't need help the network part, just the packing and unpacking)

For example you have:
byte[] vals = { 1, 0 , 1, 0, 1, 0, 1, 1 };

How would you pack vals into a single byte, then unpack it and get a byte array?

Thanks in advance.


Not sure how this is going to help you. You went through examples and tutorials but didn't get it. And is now expecting to get it by having a specific example.

Would it not be better to review the tutorial examples, explain what you think is happening, and why it doesn't make sense -- and maybe we can set you in the right direction?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
John Carr
Greenhorn

Joined: Jan 16, 2010
Posts: 17
Henry Wong wrote:Not sure how this is going to help you. You went through examples and tutorials but didn't get it. And is now expecting to get it by having a specific example.

Would it not be better to review the tutorial examples, explain what you think is happening, and why it doesn't make sense -- and maybe we can set you in the right direction?

Henry

Because I learn best via example, and every example I've seen has nothing to do with anything. They're just rubbish.

Like, I understand the shifts, it's just the bitwise operators. If I where able to actually see what the bits where it would be easier for me, but I haven't found a way to do that without continually converting the bytes value to binary via a website.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19004
    
  40

John Carr wrote:Because I learn best via example, and every example I've seen has nothing to do with anything. They're just rubbish.


And we can't help you, if we don't know what you are confused about.

What happens if we give you another example, and you don't understand it? Is it to be dismissed as rubbish too?

Henry
John Carr
Greenhorn

Joined: Jan 16, 2010
Posts: 17
Henry Wong wrote:And we can't help you, if we don't know what you are confused about.

What happens if we give you another example, and you don't understand it? Is it to be dismissed as rubbish too?

Henry

I'm confused about bitwise operations and modifying values in a primitive data type.

I don't dismiss examples as rubbish just because I don't understand them, they are rubbish because they are not explained well, nor do they have a practical application.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19004
    
  40

John Carr wrote:I'm confused about bitwise operations and modifying values in a primitive data type.


Then this is a good place to start. What are you confused about? Gives us an example, and explain to us what is confusing. Asking for code example, for a specific example, is no different than a tutorial example. If you don't believe me, here is the answer to your question....



John Carr wrote:
Say I have 8 values that are true or false and I want to store these in a single byte, send it across a network. Then, unpack it and use it to toggle 8 different things. (I don't need help the network part, just the packing and unpacking)

For example you have:
byte[] vals = { 1, 0 , 1, 0, 1, 0, 1, 1 };

How would you pack vals into a single byte, then unpack it and get a byte array?




Henry
Siddhesh Deodhar
Ranch Hand

Joined: Mar 05, 2009
Posts: 118
Hi John, let me help you as per how I have understood your difficulty.

So you want to transfer some bits from one place to other and at the other place you want to
separate those bits so that you can use each bit and do some meaningful work out of it.

So suppose you have a byte say
byte signal = 10101011

you want to transfer this and get the digits seperated i.e. 1, 0 , 1, 0, 1, 0, 1, 1

So do one thing

when you get signal at the other end, apply this algorithm

signal MOD 2 - > gives you last digit i.e. 1
signal DIV2 MOD 2 - > gives you last digit i.e. 1
signal DIV2 DIV2 MOD 2 - > gives you last digit i.e. 0

--- and so on.

Good, Better, Best, Don't take rest until, Good becomes Better, and Better becomes Best.
Sidd : (SCJP 6 [90%] )
John Carr
Greenhorn

Joined: Jan 16, 2010
Posts: 17
Thanks Henry, that actually helped me a lot.

I wrote an unpacker based on the packer, here is the code:

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Byte Packing/Unpacking