Assh Khan wrote:Following is my test code...
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Campbell Ritchie wrote:Also: why is it a problem that a byte is signed?
Campbell Ritchie wrote:If you are casting to a byte, the & 0xff part is redundant.
Winston Gutkowski wrote:
Assh Khan wrote:Following is my test code...
First: Answer Campbell's question - What is going wrong?
Second: Why not make your method
public static byte intToBytes(int value) { ...
at least while you're testing. Then you can pass values to it.
Third: bytes are always signed in Java. So are ints.
But you can convert a byte - even if it's value is negative - to a non-negative int.
Are you sure that's not what you actually want to do?
HIH
Winston
Assh Khan wrote:
I tried it using ByteBuf but the result in bytes is different from the actual value. Java returns it as signed byte, But I need unsigned bits for further bit manipulation.
Thank you.
Assh Khan wrote:I have an unsigned integer value, let say (5)...
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
But there is no such thing as an unsigned byte or an unsigned int in Java®.Daniel Cox wrote:The following code converts the unsigned byte 255 to the signed byte -1 and then converts the signed byte -1 to the unsigned byte 255. . . .
Paweł Baczyński wrote:Since Java 8 you can do this using:This method does exactly the same thing as Winston's code:
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Winston Gutkowski wrote:Is that an exact copy of the source code?
Campbell Ritchie wrote:Maybe, but unnecessary.
Daniel Cox wrote:I agree, it’s usually unnecessary for a Java program to bother whether a byte has a sign or not but every now and then, there are occasions when dealing with low level programming where a network program needs to use a method like readUnsignedByte() in the DataInputStream class to read unsigned bytes and maybe do some low level bit shifting.
I imagine these rare occasions are the reason why toUnsignedInt() was added to the Byte class in Java 8 - to allow the programmer conveniently convert signed bytes to unsigned ints.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Oh, yes, they are! That is so much better to say at the end of DecemberWinston Gutkowski wrote:. . . so in a sense bytes, chars and shorts aren't really numbers. . . .
You have to do arithmetic so attitude=100% etc. See how much arithmetical manipulation of chars and command‑line arguments you can do in the fewest lines.“So, one can conclude with mathematical certainty that While Hard work and Knowledge will get you close, and Attitude will get you there, it’s the Bullshit and Ass kissing that will put you over the top.”
Campbell Ritchie wrote:See how much arithmetical manipulation of chars and command‑line arguments you can do in the fewest lines.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters? |