Easy to Learn Java: Programming Articles, Examples and Tips

Start with Java in a few days with Java Lessons or Lectures


Code Examples

Java Tools

More Java Tools!

Java Forum

All Java Tips


Submit News
Search the site here...
Search the JavaFAQ.nu
1000 Java Tips ebook

1000 Java Tips - Click here for the high resolution copy!1000 Java Tips - Click here for the high resolution copy!

Java Screensaver, take it here

Free "1000 Java Tips" eBook is here! It is huge collection of big and small Java programming articles and tips. Please take your copy here.

Take your copy of free "Java Technology Screensaver"!.

How to unsign signed bytes in Java - your guide is here

JavaFAQ Home » General Java Go to all tips in General Java

Bookmark and Share

How to avoid problems with signed bytes? Unsign them.

We know that Java has signed bytes, we know Java, some people do not, many programs are not aware about Java at all and send unsigned bytes to Java program which waits for signed ones... what happens with your program? Whom your chief is going to blame? Of cause not James Gosling - YOU Smile

Yesterday I was recommending to one newbie in Java the "Identifiers and primitive data types" lecture as a good start with Java data types. But suddenly he asked me about signed bytes and how he is supposed to deal with them - in many other languages they are not signed. If someone writes a Java program which communicates with non Java program and sends bytes, so what happens?

Practical solution for Java signed bytes is below

Yes, maybe it is strange, maybe it is erratic, maybe it is bad. But it is just like this - Java has bytes, shorts, ints, and longs that are all signed.

-128...+127 - bytes range

the 8th bit which is (most significant) is used as a sign bit. 1 means 'negative', and 0 - 'positive'.

For example -128 is : 10000000
-125 is presented like so: 10000011 and so on.

Here is Java code example I use in my programs when I need to convert signed Java bytes into unsigned 'non Java' bytes. The code below changes range from -128... 127 to 0.... 255 and makes it possible to get "normal" integers Smile


function getUnsignedBytes(){
    byte[] bytes = getMyBytesFromSomewhere(); // here you get them here from some another place
    int length = bytes.length;
    for (int step = 0; step < length; step++){
        int byteValue = new Integer(bytes[step]).intValue( );
        if (byteValue < 0 ){
            byteValue = bytes[step] & 0x80;
            byteValue += bytes[step] & 0x7F;
    return byteValue;

 Printer Friendly Page  Printer Friendly Page
 Send to a Friend  Send to a Friend

.. Bookmark and Share

Search here again if you need more info!
Custom Search

Home Code Examples Java Forum All Java Tips Books Submit News, Code... Search... Offshore Software Tech Doodling

RSS feed Java FAQ RSS feed Java FAQ News     

    RSS feed Java Forums RSS feed Java Forums

All logos and trademarks in this site are property of their respective owner. The comments are property of their posters, all the rest 1999-2006 by Java FAQs Daily Tips.

Interactive software released under GNU GPL, Code Credits, Privacy Policy