Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

converting simple C code to Java

 
Norman Meister
Ranch Hand
Posts: 52
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Henry Wong
author
Marshal
Pie
Posts: 21498
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
Winston Gutkowski
Bartender
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 52
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i think it is better to write a simple java program instead of decoding this C code.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic