Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Convert small C routine to Java

 
Stefan Geelen
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This looks good, except that you need a class declaration. Java forces you to put everything inside of classes.

This prints -104.
 
Stefan Geelen
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 11911
207
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Stefan Geelen
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic