jQuery in Action, 2nd edition*
The moose likes Java in General and the fly likes Convert small C routine to Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Convert small C routine to Java" Watch "Convert small C routine to Java" New topic
Author

Convert small C routine to Java

Stefan Geelen
Ranch Hand

Joined: Nov 19, 2001
Posts: 49
Hi,
I have problems in converting following C routine to a Java routine:

I have following code in Java

CODE]
private static int r = 55665 ;
private static final int c1 = 52845 ;
private static final int c2 = 22719 ;
private static byte decrypt(byte cipher) {
byte plain;
if (cipher < 0)
plain = (byte)(cipher ^ (r>>>8));
else
plain = (byte)(cipher ^ (r>>8));

r = (byte) ((cipher + r) * c1 + c2) % 65536;
return plain;
}[
[/CODE]
Appareantly this is not correct.
I especially have some difficulties in define an 'unsigned short int' in Java.
Can someone give me the correct Java code of this piece of C code ?
Thank very much,
Stefan
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1824

This looks good, except that you need a class declaration. Java forces you to put everything inside of classes.

This prints -104.


Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Stefan Geelen
Ranch Hand

Joined: Nov 19, 2001
Posts: 49
Dear Joel,
thanks for the fast response.
Yes, I'm aware I need to put this in a class, but left that out just to save some space.
My question was if I correctly transformed the unsigned short int in C to a signed int in Java, or this is wrong and should be something else ?
Stefan
Bart
Greenhorn

Joined: Oct 10, 2003
Posts: 3
unsigned char from C is not the same as byte in Java.
byte in Java is signed. Not sure if this is what is
causing your problems but it is definitely not equivalent.
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11503
    
  95

Hi Bart,
Welcome to JavaRanch.
We don't have many rules here, but one we do have is our Official Policy On Registered Names. This requires you to have both a first and last name in your displayed name. Could you please add a last name to your displayed name? You can do that here.
Thanks,
Andrew


The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Stefan Geelen
Ranch Hand

Joined: Nov 19, 2001
Posts: 49
Deart Bart,
thanks for the reply.
In C the unsigned char is used to read a byte from a file.
In my program I do as follows (code fragment):

Is reading bytes this way from a file not correct ?
Are the bytes in the b array signed or unsigned ?
Stefan
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1824

In Java, all data types except for char are signed. If you need greater positive values, use the next-size-up data type. If you need unsigned bytes, use chars.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Convert small C routine to Java