 |
AppletTalk.com Java discussions newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
J. David Boyd Guest
|
Posted: Wed Jul 27, 2005 9:05 pm Post subject: need an unsigned int in java, so... |
|
|
what's the workaround?
I've got some legacy C code that I need to convert to java, and some of it
depends on unsigned ints.
The code is doing some kind of stupid crc and xor to encrypt the password
entered by a user.
When I convert it to java, and run it, sometimes the summation variables go
negative, changing some of the values in my encrypted string.
Any pointers, ideas, or URLs as to what to do to get around this?
Dave in Largo, FL
p.s. I've had no luck with google...
|
|
| Back to top |
|
 |
Knute Johnson Guest
|
Posted: Thu Jul 28, 2005 3:32 am Post subject: Re: need an unsigned int in java, so... |
|
|
J. David Boyd wrote:
| Quote: | what's the workaround?
I've got some legacy C code that I need to convert to java, and some of it
depends on unsigned ints.
The code is doing some kind of stupid crc and xor to encrypt the password
entered by a user.
When I convert it to java, and run it, sometimes the summation variables go
negative, changing some of the values in my encrypted string.
Any pointers, ideas, or URLs as to what to do to get around this?
Dave in Largo, FL
p.s. I've had no luck with google...
|
Use a long.
--
Knute Johnson
email s/nospam/knute/
|
|
| Back to top |
|
 |
J. David Boyd Guest
|
Posted: Thu Jul 28, 2005 12:53 pm Post subject: Re: need an unsigned int in java, so... |
|
|
Knute Johnson <nospam (AT) ljr-2 (DOT) frazmtn.com> writes:
| Quote: | J. David Boyd wrote:
what's the workaround?
I've got some legacy C code that I need to convert to java, and some
of it
depends on unsigned ints.
The code is doing some kind of stupid crc and xor to encrypt the
password
entered by a user.
When I convert it to java, and run it, sometimes the summation
variables go
negative, changing some of the values in my encrypted string.
Any pointers, ideas, or URLs as to what to do to get around this?
Dave in Largo, FL
p.s. I've had no luck with google...
Use a long.
|
I need the number to wrap around, using a long it just keeps on going...
|
|
| Back to top |
|
 |
Patricia Shanahan Guest
|
Posted: Thu Jul 28, 2005 1:08 pm Post subject: Re: need an unsigned int in java, so... |
|
|
J. David Boyd wrote:
| Quote: | Knute Johnson <nospam (AT) ljr-2 (DOT) frazmtn.com> writes:
J. David Boyd wrote:
what's the workaround?
I've got some legacy C code that I need to convert to java, and some
of it
depends on unsigned ints.
The code is doing some kind of stupid crc and xor to encrypt the
password
entered by a user.
When I convert it to java, and run it, sometimes the summation
variables go
negative, changing some of the values in my encrypted string.
Any pointers, ideas, or URLs as to what to do to get around this?
Dave in Largo, FL
p.s. I've had no luck with google...
Use a long.
I need the number to wrap around, using a long it just keeps on going...
|
At the end, and before any operation that shifts information to the
right, bitwise & with ((1L<<32)-1).
Patricia
|
|
| Back to top |
|
 |
googmeister@gmail.com Guest
|
Posted: Thu Jul 28, 2005 2:23 pm Post subject: Re: need an unsigned int in java, so... |
|
|
J. David Boyd wrote:
| Quote: | what's the workaround?
I've got some legacy C code that I need to convert to java, and some of it
depends on unsigned ints.
The code is doing some kind of stupid crc and xor to encrypt the password
entered by a user.
When I convert it to java, and run it, sometimes the summation variables go
negative, changing some of the values in my encrypted string.
Any pointers, ideas, or URLs as to what to do to get around this?
|
I think you should be able to get this to work with regular signed
integers.
What part fails with signed integers? Bitwise and, xor, and + work the
same for signed and unsigned int (i.e., you get the same bits), so it
shouldn't
matter that Java only has signed. On the other hand right shifting is
different - be sure to use >>> instead of >>. (The remainder and
comparison operators are also different for signed and unsigned types,
but you shouldn't need this in CRC code.)
|
|
| Back to top |
|
 |
J. David Boyd Guest
|
Posted: Thu Jul 28, 2005 3:06 pm Post subject: Re: need an unsigned int in java, so... |
|
|
[email]googmeister (AT) gmail (DOT) com[/email] writes:
| Quote: | J. David Boyd wrote:
what's the workaround?
|
Thanks all, for the tips.
I got the ops I wanted by, (and this might be stupidly redundant, but)...
I used a long (call it LL),
LL <<= 32;
LL >>>= 32;
LL &= 0x00000000FFFFFFFF;
This worked fine.
Dave
|
|
| Back to top |
|
 |
chris.ebert@gmail.com Guest
|
Posted: Thu Jul 28, 2005 5:38 pm Post subject: Re: need an unsigned int in java, so... |
|
|
A not-elegant workaround: Most C integers are 32 bits. You can do the
math in a Java long (64 bits) and mask it off. Not pretty, but I've
used the trick before.
|
|
| Back to top |
|
 |
J. David Boyd Guest
|
Posted: Thu Jul 28, 2005 8:35 pm Post subject: Re: need an unsigned int in java, so... |
|
|
[email]chris.ebert (AT) gmail (DOT) com[/email] writes:
| Quote: | A not-elegant workaround: Most C integers are 32 bits. You can do the
math in a Java long (64 bits) and mask it off. Not pretty, but I've
used the trick before.
|
Yes, that is what I ended up doing.
|
|
| Back to top |
|
 |
Dale King Guest
|
Posted: Tue Aug 02, 2005 12:22 am Post subject: Re: need an unsigned int in java, so... |
|
|
[email]googmeister (AT) gmail (DOT) com[/email] wrote:
| Quote: | J. David Boyd wrote:
what's the workaround?
I've got some legacy C code that I need to convert to java, and some of it
depends on unsigned ints.
The code is doing some kind of stupid crc and xor to encrypt the password
entered by a user.
When I convert it to java, and run it, sometimes the summation variables go
negative, changing some of the values in my encrypted string.
Any pointers, ideas, or URLs as to what to do to get around this?
I think you should be able to get this to work with regular signed
integers.
What part fails with signed integers? Bitwise and, xor, and + work the
same for signed and unsigned int (i.e., you get the same bits), so it
shouldn't
matter that Java only has signed. On the other hand right shifting is
different - be sure to use >>> instead of >>. (The remainder and
comparison operators are also different for signed and unsigned types,
but you shouldn't need this in CRC code.)
|
multiply and divide are also different.
--
Dale King
|
|
| Back to top |
|
 |
Patricia Shanahan Guest
|
Posted: Tue Aug 02, 2005 1:03 am Post subject: Re: need an unsigned int in java, so... |
|
|
Dale King wrote:
| Quote: | googmeister (AT) gmail (DOT) com wrote:
J. David Boyd wrote:
what's the workaround?
I've got some legacy C code that I need to convert to java, and some
of it
depends on unsigned ints.
The code is doing some kind of stupid crc and xor to encrypt the
password
entered by a user.
When I convert it to java, and run it, sometimes the summation
variables go
negative, changing some of the values in my encrypted string.
Any pointers, ideas, or URLs as to what to do to get around this?
I think you should be able to get this to work with regular signed
integers.
What part fails with signed integers? Bitwise and, xor, and + work the
same for signed and unsigned int (i.e., you get the same bits), so it
shouldn't
matter that Java only has signed. On the other hand right shifting is
different - be sure to use >>> instead of >>. (The remainder and
comparison operators are also different for signed and unsigned types,
but you shouldn't need this in CRC code.)
multiply and divide are also different.
|
Are you sure about multiply? Java multiplication seems to be a
simple binary multiply, ignoring sign:
"If an integer multiplication overflows, then the result is the
low-order bits of the mathematical product as represented in some
sufficiently large two's-complement format. As a result, if overflow
occurs, then the sign of the result may not be the same as the sign of
the mathematical product of the two operand values."
[http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html#5036]
The operations I would expect to be an issue are right shifts, division,
and remainder with right hand side not a power of two. In each case,
data flows to lower significance bits.
Patricia
|
|
| Back to top |
|
 |
Dale King Guest
|
Posted: Tue Aug 09, 2005 12:05 am Post subject: Re: need an unsigned int in java, so... |
|
|
Patricia Shanahan wrote:
| Quote: | Dale King wrote:
[email]googmeister (AT) gmail (DOT) com[/email] wrote:
What part fails with signed integers? Bitwise and, xor, and + work the
same for signed and unsigned int (i.e., you get the same bits), so it
shouldn't
matter that Java only has signed. On the other hand right shifting is
different - be sure to use >>> instead of >>. (The remainder and
comparison operators are also different for signed and unsigned types,
but you shouldn't need this in CRC code.)
multiply and divide are also different.
Are you sure about multiply? Java multiplication seems to be a
simple binary multiply, ignoring sign:
|
Actually, I was second guessing myself when I posted that, but don't
have time to test it myself.
--
Dale King
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|