File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Java » Java in General
Bookmark "Convert small C routine to Java" Watch "Convert small C routine to Java" New topic

Convert small C routine to Java

Stefan Geelen
Ranch Hand

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

I have following code in Java

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));
plain = (byte)(cipher ^ (r>>8));

r = (byte) ((cipher + r) * c1 + c2) % 65536;
return plain;
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,
Joel McNary

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 ?

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: 11778

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.

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 ?
Joel McNary

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.
It is sorta covered in the JavaRanch Style Guide.
subject: Convert small C routine to Java
It's not a secret anymore!