Answer the question
In order to leave comments, you need to log in
Translation from C to Java. Pointers?
Hello!
There is a program in C and I really want to rewrite it in Java.
The languages are very similar so in most cases you can even copy-paste. But there was a problem with pointers. In Java they don't exist at all, but in C they are used all the time.
How to be in this case?
Here is an example of the code that you want to translate.
int decode(int correct_mode, int *errs, unsigned long *cw) <br>
/* This function decodes codeword *cw in one of two modes. If correct_mode <br>
is nonzero, error correction is attempted, with *errs set to the number of<br>
bits corrected, and returning 0 if no errors exist, or 1 if parity errors <br>
exist. If correct_mode is zero, error detection is performed on *cw, <br>
returning 0 if no errors exist, 1 if an overall parity error exists, and <br>
2 if a codeword error exists. */ <br>
{ <br>
unsigned long parity_bit; <br><br>
if (correct_mode) /* correct errors */ <br>
{ <br>
parity_bit=*cw & 0x800000l; /* save parity bit */ <br>
*cw&=~0x800000l; /* remove parity bit for correction */<br><br>
*cw=correct(*cw, errs); /* correct up to three bits */ <br>
*cw|=parity_bit; /* restore parity bit */ <br><br>
/* check for 4 bit errors */ <br>
if (parity(*cw)) /* odd parity is an error */ <br>
return(1); <br>
return(0); /* no errors */ <br>
} <br>
else /* detect errors only */ <br>
{ <br>
*errs=0; <br>
if (parity(*cw)) /* odd parity is an error */ <br>
{ <br>
*errs=1; <br>
return(1); <br>
} <br>
if (syndrome(*cw)) <br>
{ <br>
*errs=1; <br>
return(2); <br>
} <br>
else <br>
return(0); /* no errors */ <br>
} <br>
} /* decode */ <br>
Answer the question
In order to leave comments, you need to log in
This is the place
int decode(int correct_mode, int *errs, unsigned long *cw)
public int[] decode(int correct_mode, int errs, int cw);
int[] arr = int long[3];
arr[1]=2;
arr[1]=errs;
arr[2]=cs;
return arr;
return 0 and return 1 are similar. int[] arr = decode(correct_mode, errs, cw);
result = arr[0];
errs = arr[1];
cw = arr[2];
It seems like he didn't forget anything. In Java, parameters to methods are passed by value. In the case of primitive types, the value itself is passed, in the case of classes, a reference to the instance. there are no out parameters.
Options:
a) Rewrite the implementation so that there are no output parameters. For example, create a class where the original decode function and all auxiliary functions become methods, and the parameters become class fields with getters / setters.
b) Apply the following trick:
public class ByRef<T> {
private T ref;
public ByRef(T ref) {
set(ref);
}
public T get() {
return this.ref;
}
public void set(T ref) {
this.ref = ref;
}
}
public class Foo {
public static void bar(ByRef<String> value) {
value.set("Good bye!");
}
public static void main(String... args) {
ByRef<String> message = new ByRef<String>("Hello!");
System.out.println("Before call: " + message.get());
bar(message);
System.out.println("After call: " + message.get());
}
}
In your code, pointers act like links, but JAVA has links. Bitwise operations are also available. So I don't see a problem in rewriting this case in JAVA
int decode(int corect_mode, ref int errs, ref long cw)
{
//остальной код
//...
else {
errs = 0;
if(parity(cw))
{
errs = 1;
return 0
}
//Остальной код
}
}
Instead of perverting and modeling output parameters, return the value cw, and signal the error with an expression.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question