• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Convert small C routine to Java

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author and jackaroo
Posts: 12200
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic