14 Replies Latest reply: Jan 9, 2008 7:18 PM by 796440 RSS

    converting bytes to unsigned bytes

    807603
            int u=0;
            int i=32;
      
       byte []  data1 = new byte[1024];
       int[] unsignedValue=new int[1024];
      
      while(u<100){
                         
                          unsignedValue[u] =data1[i] & 0xff;
      
                            u++;
                            i++;
                         } 
      from input stream iam storing bytes in data1.now i want to convert specific bytes to unsigned bytes.

      here i tryed in the above way but i want to convert
      2 to 8
      12 to 18
      22 to 28
      bytes to unsigned and i want to store how can i do this?
        • 1. Re: converting bytes to unsigned bytes
          807603
          in my previous post i mistakenly gave i=32 but ( i=0);
          • 2. Re: converting bytes to unsigned bytes
            796440
            There's no such thing as an unsigned byte in Java, and for many operations, the results are the same for signed and unsigned.

            Can you clarify what you're trying to do?
            • 3. Re: converting bytes to unsigned bytes
              807603
              sorry in my documentation they gave that incomming bytes are unsigned char .im storing all bytes in buffer ie data1.some bytes
              2 to 8
              12 to 18
              22 to 28
              are in negative ..
              • 4. Re: converting bytes to unsigned bytes
                807603
                nanda_java wrote:
                sorry in my documentation they gave that incomming bytes are unsigned char
                Oh goodie, yet another datatype.

                You are aware that bytes and chars are not the same thing?

                Perhaps you could post a snippet of this documentation that might explain what you are trying to do?
                • 5. Re: converting bytes to unsigned bytes
                  796440
                  nanda_java wrote:
                  sorry in my documentation they gave that incomming bytes are unsigned char .im storing all bytes in buffer ie data1.some bytes
                  2 to 8
                  12 to 18
                  22 to 28
                  are in negative ..
                  0xFE is 0xFE. It's neither signed nor unsigned, neither positive nor negative. If you need to interpret it as 254 instead of -2, you can't use a Java byte. You have to, for example, AND it with the int 0xFF.
                  • 6. Re: converting bytes to unsigned bytes
                    807603
                    in documentation they gave that input is unsigned char in special format .but i know that in java there is no
                    unsigned char


                    i am getting some input bytes as negative .its clear that they are not in byte range .
                    • 7. Re: converting bytes to unsigned bytes
                      807603
                      nanda_java wrote:
                      in documentation they gave that input is unsigned char in special format .but i know that in java there is no
                      unsigned char


                      i am getting some input bytes as negative .its clear that they are not in byte range .
                      * ahem *

                      Let me try this again.

                      BYTES ARE NOT CHARS!

                      CHARS ARE NOT BYTES!

                      Now there are no unsigned chars either but I really don't think it's very good for your results to be trying to read chars as bytes. In fact I know it's bad.
                      • 8. Re: converting bytes to unsigned bytes
                        807603
                        cotton.m wrote:
                        BYTES ARE NOT CHARS!

                        CHARS ARE NOT BYTES!
                        If the documentation OP is referring to is C-oriented, it could be.

                        OP: Since there may be some confusion, refer to their size (8-bit, 16-bit, 32-bit, etc).
                        • 9. Re: converting bytes to unsigned bytes
                          807603
                          i know every thing bytes are not char but my intension is in java there are only signed bytes from -127 to 127

                          soi want to convert these specific bytes as 0 to 255 ..
                          so how can i do these specific bytes

                          2 to 8
                          12 to 18
                          22 to 28
                          which are in negative to express as 0 to 255.
                          • 10. Re: converting bytes to unsigned bytes
                            807603
                            nanda_java wrote:
                            i know every thing bytes are not char but my intension is in java there are only signed bytes from -127 to 127
                            That's -128 to 127.
                            soi want to convert these specific bytes as 0 to 255 ..
                            No conversion is necessary as long as you leave it in a byte. The key thing to understand is that in an 8-bit field, the unsigned value 255 is the same as the signed value -1. When you store it in an "int", it undergoes numeric promotion, and consequently, sign-extension. At this point, you can mask it using 0xFF (see reply #5) to remove the sign-extended bits, leaving the original 8 bits you started with.

                            EDIT: Have a look at http://en.wikipedia.org/wiki/Two%27s_complement
                            • 11. Re: converting bytes to unsigned bytes
                              baftos
                              Let's take the byte 11110000. This is language independent. Hex F0.
                              If you store it in a C unsigned byte, it will represent the value 240 dec.
                              If you store it in a C signed byte or in a java byte, it will represent the
                              value -16. No way around it. If you want to use it as 240 in some Java
                              arithmetics, you better use a larger data type, for instance short.
                              But careful! Make sure that the MSB is all zeros, otherwise you will
                              still end up with a negative short. How you do this? You mask it
                              with 0x00FF like in:
                              byte b = readTheByteSomehow(); // -128 to 127
                              short s = b & 0x00ff; // 0 to 255
                              • 12. Re: converting bytes to unsigned bytes
                                807603
                                in my fist post only i multiplyes with 0xff .and i gave code ...my question is how to multiply only specific
                                2 to 8
                                12 to 18
                                22 to 28

                                bytes with 0xff .the bytes are in data1.
                                • 13. Re: converting bytes to unsigned bytes
                                  807603
                                  nanda_java wrote:
                                  in my fist post only i multiplyes with 0xff .and i gave code ...my question is how to multiply only specific
                                  2 to 8
                                  12 to 18
                                  22 to 28

                                  bytes with 0xff .the bytes are in data1.
                                  How about you look up the definition of multiply and come back with a better question.
                                  • 14. Re: converting bytes to unsigned bytes
                                    796440
                                    nanda_java wrote:
                                    in my fist post only i multiplyes with 0xff .and i gave code ...my question is how to multiply only specific
                                    2 to 8
                                    12 to 18
                                    22 to 28

                                    bytes with 0xff .the bytes are in data1.
                                    I have no idea what you're saying, and I have no idea what the significance of those ranges is.

                                    Look, you've already been told this twice: Say you get the byte 0xFE. That byte is neither signed nor unsigned. Get that into your head. It's simply a byte. The concept of signed/unsigned only comes into play when you decide to treat that as an integer value. You might want it to represent -2, or you might want it to represent 254. If you store it in a Java byte, it will represent -2. You cannot change that. If you want 0xFE to mean 254, then mask it with 0xFF and store it in an int.
                                    byte b = -2;
                                    System.out.println(b & 0xFF); // prints 254
                                    Furthermore, as already stated, in many cases it doesn't matter if it's signed or unsigned. If you do addtion, subtraction, multiplication (I think), AND, OR, XOR, you'll get the same byte result, regardless of whether you consider it signed or unsigned, and again, signed/unsigned only matters if you care about the numerical value of that signed result, rather than simply its bit pattern. 0xFE - 0x01 = 0xFD, and you can take that as -2 -1 = -3 or as 254 - 1 = 253. The bytes are the same regardless.

                                    What part are you not understanding? Don't just keep repeating the same thing about how it's unsigned and 2 to 8 and all that nonsense.

                                    So please, define your problem more clearly.

                                    Edited by: jverd on Jan 10, 2008 8:53 AM