This content has been marked as final. Show 14 replies
nanda_java wrote: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.
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 ..
nanda_java wrote:* ahem *
in documentation they gave that input is unsigned char in special format .but i know that in java there is no
i am getting some input bytes as negative .its clear that they are not in byte range .
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.
nanda_java wrote:That's -128 to 127.
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 ..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
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
nanda_java wrote:I have no idea what you're saying, and I have no idea what the significance of those ranges is.
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.
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.
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.
byte b = -2; System.out.println(b & 0xFF); // prints 254
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