Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Unsigned types

Status
Not open for further replies.

rotovegasBoy

Programmer
Sep 16, 1999
88
NZ
Hi
Write embedded systems and have a lot of use for bit masks shifting etc. I wrote a java app that acts as a calculator and can take a hexadecimal number and tell me what bits are set i've added masking and shifting etc. What i what to do is get the output in decimal as well as hex but of course if i have a 1 in the top bit of an interger it becomes a negative number. The systems i write use C and inputing hex numbers is fine, but for some code its more readible if i use decimal does java have anyway of handling unsigned types? "There are no stupid questions, just lots of inquisitive idiots" - anon
 
I don't quite understand what you're trying to do.

The way to get a systematically positive number would be to call the overloaded Math.abs() method on your result.

Also, remember that while the shift left (<<) always fills with zeros, when shifting right you can fill with the sign bit or zeros (>> and >>>).

HTH
 
To be direct, NO Java has no unsigned types. (This is one of the things the bugs me once in a while hehe)

I to am not exactly sure what you are trying to do, but to take a guess you may want to look at
Code:
Integer.toHexString(int i) 
Integer.parseInt(String s, int radix)

If this does not help maybe you could post a little more detail about what you are trying to do. :)

Rodney
 
basically what i'm trying to do is have output like

binary: 11111111111.....
hex: FFFFFFFF
decimal: 49388222 (whatever it is) not -123445677

but from looking at things i dont think that java does it. also the java.lang.Math class cant be extended because its final (i tried it with java 1.2, but i can't see why they would change it)
&quot;There are no stupid questions, just lots of inquisitive idiots&quot; - anon
 
RotovegasBoy,

Then the what I said should work for you. Try these static methods in the Integer class:
Code:
Integer.toHexString(int i) 
Integer.toBinaryString(int i) 
Integer.toOctalString(int i) 
Integer.toString(int i, int radix) 
Integer.parseInt(String s, int radix)
The best are the last two, they allow you provide a your own radix. So if you want to do something other than binary (radix 2), octal (radix 8), or hex (radix 16) which are built in, you can provide your radix.

They should work for you. If they do not for some reason, let me know. A while back I wrote my own Hex converter for kicks just to do it. But in real life applications I use the above methods to handle the conversions when needed.

Rodney
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top