M
M
MrMeison2012-05-26 13:26:24
Java
MrMeison, 2012-05-26 13:26:24

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>

Thanks in advance for your help.
PS If you're interested, I'm trying to implement the Golay code. More details can be found here: www.aqdi.com/golay.htm . I would be very grateful if someone throws references to implementation libraries, if there are any.

Answer the question

In order to leave comments, you need to log in

5 answer(s)
K
kostik450, 2012-05-26
@MrMeison

This is the place

int decode(int correct_mode, int *errs, unsigned long *cw)

do this way:
public int[] decode(int correct_mode, int errs, int cw);

Accordingly, inside the function, instead of
*errs and *cs
, do without stars, that is, like this:
errs and cs
well, at the end of the function, where return (2), we return like this:
int[] arr = int long[3];
arr[1]=2;
arr[1]=errs;
arr[2]=cs;
return arr;
return 0 and return 1 are similar.
And we make the call like this:
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 short, we return the result as an array, and then we deserialize the array.
It is possible, by the way, and initially to transfer in the form of an array,
inside this array to “twist” and return it as a result.
Or you can not transfer or return anything at all, everything is inside the class, that is, in the end we will come to what the respected Maccimo wrote in option a)
But if the task is to stupidly rewrite the C ++ code in Java without refactoring, then my method more ideal.

M
Maccimo, 2012-05-26
@Maccimo

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;
  }

}


Usage example:
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());
  
  }

}


But option "a" is still more ideologically correct.

S
Sergey, 2012-05-26
Protko @Fesor

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     
        }
        //Остальной код
    }

}

K
Kaigorodov Alexey, 2012-05-28
@rfq

Instead of perverting and modeling output parameters, return the value cw, and signal the error with an expression.

M
MrMeison, 2012-05-26
@MrMeison

It still doesn't work :(
It throws an error when decode method is declared:
Multiple markers at this line
- Syntax error on token "int", delete this
token
- Syntax error on token "long", delete
this token

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question