We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Apologies in advance if it's just me having a bad brain day, but I'm getting some rather odd results with this sketch:
void setup()
{
Integer myInteger = 0x23456789;
println("myInteger in hex=" + hex(myInteger)); // Just using hex()
println("myHexFromCast =" + hex((int)myInteger, 8)); // hex of an int cast
println("myHexFromMethod =" + hex(int(myInteger), 8)); // hex of the int() method
}
Yeah, I realize (now) that int(myInteger) is wrong given that int() is just for primitive data types, but I would have thought that something somewhere would have yelped in pain that I even tried to do it.
The output I get is: myInteger in hex=23456789 myHexFromCast =23456789 myHexFromMethod =23456780
So no error, but a subtly wrongo result.
If you change the initial value of myInteger to 0x2345FFFF you get:
myInteger in hex=2345FFFF
myHexFromCast =2345FFFF
myHexFromMethod =23460000
It has the hallmarks of either some weird conversion, some kind of binary format issue, with a tad of a rounding error thrown in (rounding for integers?) Or it's just my addled brain with a parity error.
Anyone know what's going on, please?
Andy. :)
Answers
@ajohnsonlaird -- I believe that you don't need to cast
Integer
toint
due to autounboxing. Also keep in mind that you canprintln(Integer)
directly because the method is overloaded.All of these things work:
The problem you are experiencing with
int(Integer i)
has nothing to do with hex specification orhex()
conversion -- that just complicates the examples. Likewise, (int) casting is fine. Using the Processing functionint()
on anInteger
is the problem:It is possible that a bug (or documentation caveat) should be filed against Processing
int()
... maybe someone has more insight into why this might be expected? For more on Java Integer -> int in general beyond Processing, see:Integer class has the method
Integer.intValue()
that returns an int with the value of the Integer.Thanks for the responses. I only tripped over this problem because of my ineptitude but I was surprised that it (using int() on an Integer) didn't provoke an error, but just returned an incorrect value.
Thanks LOTG for the tip re: that intValue() method....
Andy.