is there a way to convert unsigned integer to signed integer in java?

Printable View

- 05-16-2010, 12:41 PMdiskhubconvert unsigned integer to signed integer in java?
is there a way to convert unsigned integer to signed integer in java?

- 05-16-2010, 12:57 PMJosAH
- 05-16-2010, 01:28 PMdiskhub
i wish to do the following.

i managed to do some computation to get to the following hexi

0x8765 ==> 1000 0111 0110 0101

but i wish to convert it to

0xFFFF8765 ==> 1111 1111 1111 1111 1000 0111 0110 0101

but if it is

0x4321 ==> 0100 0011 0010 0001

i wish to produce the same

0x4321 ==> 0000 0000 0000 0000 0100 0011 0010 0001

How can i do it without much steps... - 05-16-2010, 01:59 PMJosAH
- 05-16-2010, 02:29 PMdiskhub
thanks. it works

short is 16 bits?

can u guide me on how by converting to short will make this work? - 05-16-2010, 06:50 PMJosAH
Yep, a short has 16 bits. Converting it to an int uses sign extension when applicable; have a look:

Code:`int source= 0x4123;`

short ssource= (short)source;

int target= ssource; // == 0x00004123

int source= 0x8123;

short ssource= (short)source;

int target= ssource; // == 0xffff8123

Jos - 05-17-2010, 01:50 AMdiskhub
it is surprisingly that short will pick up the 16th binary and convert the front bits according to whether it is 1 or 0...

Does this apply to all other datatype?

And what if it's vice versa, when it's 16bit changing to 32?

it will also see the 16th bit and change accordingly?