• Post Reply Bookmark Topic Watch Topic
  • New Topic

What is the Java equivalent of this C code?  RSS feed

 
Ravi Shankarappa
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is a C code that I came across, and am not able what to make of the "for" loop. Could some one tell me what is the Java equivalent code look like?



Thanks in advance

-Ravi
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The C for loop is the same as a java for loop


This code unfortunately relies on the fact that in C a 0 means FALSE, and non-0 means TRUE. So in the loop


The loop runs until the i-- statement returns 0. The Java equivalent is probably


[edit] Though I wouldn't be surprised if my java version is off-by-on
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve Luke wrote:[edit] Though I wouldn't be surprised if my java version is off-by-on

I assume you mean "off-by-one"
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:
Steve Luke wrote:[edit] Though I wouldn't be surprised if my java version is off-by-on

I assume you mean "off-by-one"


That would be correct. I meant off-by-one
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve Luke wrote:The C for loop is the same as a java for loop


This code unfortunately relies on the fact that in C a 0 means FALSE, and non-0 means TRUE. So in the loop


The loop runs until the i-- statement returns 0. The Java equivalent is probably


[edit] Though I wouldn't be surprised if my java version is off-by-on

There is one problem indeed. In the C code, i is decreased at the start of the loop. In your code it's decreased at the end of the loop. The solution is easy: let i start one lower:
That just leaves one issue: the conversion of unsigned char (which actually takes 1 byte) to something Java supports.
 
Ravi Shankarappa
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to everyone. The C code basically takes care of "endian" issue. So I just used the following Java code for my purpose.



 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's a standard method that does exactly that: Integer.reverseBytes()
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve Luke wrote:
fred rosenberger wrote:
Steve Luke wrote:[edit] Though I wouldn't be surprised if my java version is off-by-on

I assume you mean "off-by-one"


That would be correct. I meant off-by-one

Very illustrative of what you meant :)
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!