Asked  7 Months ago    Answers:  5   Viewed   29 times

I am trying to convert a signed byte in unsigned. The problem is the data I am receiving is unsigned and Java does not support unsigned byte, so when it reads the data it treats it as signed.

I tried it to convert it by the following solution I got from Stack Overflow.

public static int unsignedToBytes(byte a)
{
    int b = a & 0xFF;
    return b;
}

But when again it's converted in byte, I get the same signed data. I am trying to use this data as a parameter to a function of Java that accepts only a byte as parameter, so I can't use any other data type. How can I fix this problem?

 Answers

65

I'm not sure I understand your question.

I just tried this and for byte -12 (signed value) it returned integer 244 (equivalent to unsigned byte value but typed as an int):

  public static int unsignedToBytes(byte b) {
    return b & 0xFF;
  }

  public static void main(String[] args) {
    System.out.println(unsignedToBytes((byte) -12));
  }

Is it what you want to do?

Java does not allow to express 244 as a byte value, as would C. To express positive integers above Byte.MAX_VALUE (127) you have to use a different integral type, like short, int or long.

Tuesday, June 1, 2021
 
Sagar
answered 7 Months ago
62

I don't believe so. Once you want to go bigger than a signed long, I think BigInteger is the only (out of the box) way to go.

Wednesday, June 9, 2021
 
Wickethewok
answered 7 Months ago
25

HTML has no built-in "print" action for an anchor tag or input button (or anything else), so JavaScript is your way. You could also include instructions for your users on how to print using File->Print or the other various alternatives.

Tuesday, August 31, 2021
 
Daff
answered 4 Months ago
54

A typecast has a higher precedence than the & operator. Therefore you're first casting to an int, then ANDing in order to mask out all the high-order bits that are set, including the "sign bit" of the two's complement notation which java uses, leaving you with just the positive value of the original byte. E.g.:

let byte x = 11111111 = -1
then (int) x = 11111111 11111111 11111111 11111111
and x & 0xFF = 00000000 00000000 00000000 11111111 = 255

and you've effectively removed the sign from the original byte.

Tuesday, September 14, 2021
 
Aximili
answered 3 Months ago
35

If you're referring to

(Long.compareUnsigned(l, Long.MAX_VALUE*2) < 0)

l reaches

-9223372036854775808

unsigned it is

9223372036854775808

and

Long.MAX_VALUE*2

is

18446744073709551614

So l is smaller than Long.MAX_VALUE*2 in the unsigned world.

Assuming you're asking about the 0's

0
0
0
...
0

the problem (if you see it that way) is that, for long (other numerical primitives), the first bit is the sign bit.

so

10000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000

is

-9223372036854775808

When you do

-9223372036854775808 + -9223372036854775808

you underflow (overflow?) since

    10000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
+   10000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000

is

00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000

which is 0. On later loop iterations, 0 + 0 remains 0.

Monday, November 8, 2021
 
Dimgold
answered 1 Month ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :  
Share