• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Byte Manipulation

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everybody
what are the different between inner-class and Top level nested class?
please tell anyone
 
Steven Bell
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1071
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1071
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
reply
    Bookmark Topic Watch Topic
  • New Topic