Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes Byte Manipulation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Byte Manipulation" Watch "Byte Manipulation" New topic
Author

Byte Manipulation

Yukon Cornelius
Greenhorn

Joined: Jun 15, 2005
Posts: 4
Is there a page or documentaion that shows information regarding the primitive data type byte and byte array? The API falls short and I need to manipulate unsigned bytes for a project. Project is not due for a while so I have some time but I was just looking into the subject and there is a total lack of reference material. This is probably due to the fact that no one is using primitive bytes in java and if they are they know what they are doing. Anyway, any help would be great.

Yukon ho!


Didn't I ever tell you? Bumbles Bounce!
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
What do you want to do with bytes? bytes aren't any different from ints other than they can't hold as large of numbers. By the way there are no unsigned bytes in Java.
samdeep aarzoo
Ranch Hand

Joined: Jun 09, 2005
Posts: 160
Hi everybody
what are the different between inner-class and Top level nested class?
please tell anyone
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
bikash,

You should seach the forum for answers to that question, and if you can't find anything that works for you post a new topic rather than jump into an unrelated topic with your question.
Yukon Cornelius
Greenhorn

Joined: Jun 15, 2005
Posts: 4
I am working with network communication. The java client and server must work with a c client and server under certain specifications. The instructions for my particular project are to use checksum with two unsigned bytes. I know that java doesn't have unsigned bytes. I can do most of my project with the primitive bytes and byte arrays. I cannot however think of an easy way to perform my checksum. The checksum entails adding all bytes in the array and producing the 1s complement. The 1s comp is sent as the checksum. This must be in a 2 byte short integer (unsigned) per the instructions of the project. It seems to me that it wouldn't be possible using bytes or byte arrays only because the most significant bit for a byte is the sign bit, at least for Java. I would like any documentation if known so I can research the subject. If not any suggestions would also be nice :]

If you have any ideas or need any more info let me know.

Yukon
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
Because you are getting unsigned bytes from a C program and need the value of them in a Java program I would recommend the following.

for each byte that comes into the system put it's unsigned value into an int.


If it's an array of unsigned bytes you would just do that in a loop. Then the question is does your checksum rely on the overflow behavior of the unsigned byte (rolling back to zero). If so you will have to mod the result by the largest value of the unsigned byte.

Does that help?
Yukon Cornelius
Greenhorn

Joined: Jun 15, 2005
Posts: 4
Just to clarify, ANDing with the value of 0 is going to eliminate the sign value of the byte and will represent it as an int for later use. How does that exactly work if you don't mind me asking?

The checksum does rely on the overflow behavior of the unsigned bytes. The acutal checksum consists of an int that is represented by a two byte int. The checksum will wrap only if there is overflow when adding the bytes from the entire packet. This would need to wrap between the bytes. The wrap will add the bit in the 15th or most significant to the 0th or the least significant bit and continue to add through if needed. From what I am understanding, all of the bit manipulation will have to come from int values on the Java side. Is there a way to manipulate the acutal bits where I can specify a byte other than using the int value from -128 between 127?

Yukon
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
It looks like I gave you bad advise about the &. Sorry about that.

Well, I don't think an unsigned byte acts differently than a signed by in terms of the overflow behavior, other than the value it represents. So if you just kept adding the bytes as they came in you should be ok. You would just have to do something like:


sorry again about the bad advice.
Yukon Cornelius
Greenhorn

Joined: Jun 15, 2005
Posts: 4
I appreciate any advice. It got me thinking at least. I will see what I can do with the code you provided. For now I may move on to another part of the project. Thanks though Steven.

Yukon
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Byte Manipulation