Java only knows signed values and will expand short to int. So if the first bit is the this will give a negative number when expanded to int. Just mask the short with & 0xffff if you want to get the unsigned value. Execute the following example to see what I mean:
short vendorId = (short) 0xfd13; // without the short cast the value 0xfd13 would already expand to int and give a compile error
System.out.println("signed short: " + vendorId);
int vendorIdInt = vendorId;
System.out.println("signed int: " + vendorIdInt);
System.out.println("signed int (hex): " + Integer.toHexString(vendorId));
System.out.println("unsigned short: " + (vendorId & 0xffff));
System.out.println("unsigned int: " + (vendorIdInt & 0xffff));
vendorIdInt = vendorId & 0xffff; // mask to unsigned int
System.out.println("unsigned int: " + vendorIdInt);
System.out.println("unsigned int (hex): " + Integer.toHexString(vendorIdInt));
signed short: -749
signed int: -749
signed int (hex): fffffd13
unsigned short: 64787
unsigned int: 64787
unsigned int: 64787
unsigned int (hex): fd13
Hi there,
In linux i get a product ID of "fd13" :
lsusb: Bus 001 Device 006: ID 04b4:fd13
Which is in dezimal "64787".
The return value of "Usb_Device.getDescriptor().getIdVendor()" is defined as
"public short getIdProduct()"
So the return value is "-749" It seems short is to short..
Is there another way to get the correct value? Utils.logbus(bus) does it print correct - but there is no way to get it.
Some hints?
Thanx,
Hs
Java only knows signed values and will expand short to int. So if the first bit is the this will give a negative number when expanded to int. Just mask the short with & 0xffff if you want to get the unsigned value. Execute the following example to see what I mean:
short vendorId = (short) 0xfd13; // without the short cast the value 0xfd13 would already expand to int and give a compile error
System.out.println("signed short: " + vendorId);
int vendorIdInt = vendorId;
System.out.println("signed int: " + vendorIdInt);
System.out.println("signed int (hex): " + Integer.toHexString(vendorId));
System.out.println("unsigned short: " + (vendorId & 0xffff));
System.out.println("unsigned int: " + (vendorIdInt & 0xffff));
vendorIdInt = vendorId & 0xffff; // mask to unsigned int
System.out.println("unsigned int: " + vendorIdInt);
System.out.println("unsigned int (hex): " + Integer.toHexString(vendorIdInt));
signed short: -749
signed int: -749
signed int (hex): fffffd13
unsigned short: 64787
unsigned int: 64787
unsigned int: 64787
unsigned int (hex): fd13
Also have a look at the second paragraph in this post https://sourceforge.net/forum/message.php?msg_id=4255720