aspose file tools*
The moose likes Beginning Java and the fly likes converting simple C code to Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "converting simple C code to Java" Watch "converting simple C code to Java" New topic
Author

converting simple C code to Java

Norman Meister
Greenhorn

Joined: Jul 03, 2009
Posts: 23
Hi People,
I am new to C programming and have very little idea on how to convert this following C code to Java:



Supposidily, this above code is doing a mapping of 'TT room code' to 'TRH room code', according to thsi following table:


TRH room code TT room code Binary Code
ABCD PABCD 0101 0000
aBCD AABCD 0100 0001
AbCD BABCD 0100 0010
abCD CABCD 0100 0011
ABcD DABCD 0100 0100
aBcD EABCD 0100 0101
AbcD FABCD 0100 0110
abcD GABCD 0100 0111
ABCd HABCD 0100 1000
aBCd IABCD 0100 1001
AbCd JABCD 0100 1010
abCd KABCD 0100 1011
ABcd LABCD 0100 1100
aBcd MABCD 0100 1101
Abcd NABCD 0100 1110
abcd OABCD 0100 1111

Could someone please give me soem hint on how to convert it to java?

Thanks.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8244
    
  23

Suleman Butt wrote:Could someone please give me soem hint on how to convert it to java?

Yes: Don't.

C allows you to do all sorts of nasty things that you can't do in Java, and that bit of code looks pretty nasty to me as well. What I can tell you is that a DWORD is a "double word integer" which, on most modern machines, translates to a Java long.

Otherwise; you already know what the code is supposed to be doing. I'd suggest forgetting about the C and write a piece of Java that performs the same task.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18997
    
  40

Winston Gutkowski wrote:C allows you to do all sorts of nasty things that you can't do in Java, and that bit of code looks pretty nasty to me as well. What I can tell you is that a DWORD is a "double word integer" which, on most modern machines, translates to a Java long.

Otherwise; you already know what the code is supposed to be doing. I'd suggest forgetting about the C and write a piece of Java that performs the same task.


To be fair, it isn't that bad. Java casting does, for the most part, does a good job of casting from type to type. You just have to be wary of the sign extensions which can happen, as Java types are are all signed types. It can be done easily, once you get used to it.

Futhermore, you can also use the java.nio.ByteBuffer class. This allows you to keep everything as a byte array, and treat different parts of it differently.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8244
    
  23

Henry Wong wrote:To be fair, it isn't that bad. Java casting does, for the most part, does a good job of casting from type to type. You just have to be wary of the sign extensions which can happen, as Java types are are all signed types. It can be done easily, once you get used to it.

Futhermore, you can also use the java.nio.ByteBuffer class. This allows you to keep everything as a byte array, and treat different parts of it differently.

Oh, I'm not saying it can't be done; just that "converting" doesn't seem like a very good idea to me. For one thing, whenever you're dealing with binary, C will use the byte-ordering of the local machine unless specifically coded otherwise; Java's is always big-endian, unless you go to some lengths to make it not. The above "translation" exercise is therefore not simple if you want it to work universally, and that's all I was trying to tell Suleman.

However, I think we've got too small a snippet of the actual code to make any real determination.

Winston
Norman Meister
Greenhorn

Joined: Jul 03, 2009
Posts: 23
i think it is better to write a simple java program instead of decoding this C code.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: converting simple C code to Java