• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Exact replica of CRC code from C to Java

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is a C application that takes a string input and returns a short (0-65535) value using a CRC algorithm. I need to get the same output from a JAVA method such that the input and the output from the Java method remains the same as the C code since parts of the application cannot change. I tried google and other posts, but haven't found a solution.

The C method is below.

 
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I tried google and other posts, but haven't found a solution.



It is basically a single loop with two expressions. Besides being careful with the calculations to make sure that you don't accidently do a signed operation (as the C variables are unsigned)... How hard is it to port it?

Henry
 
Tom Davis Sr
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could someone correct the following 'for' loop, tried to replicate the while loop in the C code above.

 
Tom Davis Sr
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Any help on this please?
 
Sheriff
Posts: 22683
128
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
http://faq.javaranch.com/java/PatienceIsAVirtue

Tom Davis Sr wrote:


(c = *pName++) != 0 reads the next character of the char*, and checks if it isn't 0 (actually '\0', the NULL character).

Now, if you replace char* with String, then the following is how you usually iterate over a String:
Since char is actually numeric you can use it in your calculations without any problems.

Just for reference, you can also use a CharacterIterator:
The former method is preferred though.
 
Henry Wong
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

And as I already hinted, you really have to be careful to not do a signed operation. Keep in mind that the original C program used unsigned variables, and you are now using signed variables, which behaves differently under certain conditions.

The one that you seem to be running into is the assignment. For example, assigning a byte to an int may not be what you want in all cases...

int i = b;

If the sign bit (high bit) of the byte b is set, it will sign extend to the int -- this is not what you want. You need to be careful to remove any sign extended created bits when you do this...

int i = ((int) b) & 0xff;

Henry
 
Acetylsalicylic acid is aspirin. This could be handy too:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic