alex1256
Messages postés10Date d'inscriptionlundi 16 février 2009StatutMembreDernière intervention23 mai 2012
-
7 janv. 2012 à 13:10
alex1256
Messages postés10Date d'inscriptionlundi 16 février 2009StatutMembreDernière intervention23 mai 2012
-
8 janv. 2012 à 19:04
Bonjour,
J'essaie d'interpréter un code en c au java. Mais il me demande de changer ma condition "if ( carry )" en booléen en java et je ne sais pas comment résoudre ce problème.
voici le code en C et mon interprétation en java.
Merci d'avance pour votre aide!
alex
include <stdio.h>
#include <stdlib.h>
//#include
void main()
{
int Poly = 0x8408;
int lg = 9;
unsigned int Frame [] = {0x0B,0x20,0x0B,0x19,0x10,0x30,0x05,0x27,0x01};
unsigned int Crc;
int j, i_bits,carry;
Crc=0;
for (j=0 ; j<lg; j++)
{
Crc = Crc ^ Frame[j];
for ( i_bits=0 ; i_bits < lg-1 ; i_bits++ )
{
carry = Crc & 1;
Crc = Crc / 2;
if ( carry )
{
Crc = Crc ^ Poly;
}
}
}
printf ( "CRC = %x",Crc);
}
public class CRC16 {
public static void main(String[] args) {
long Poly = 0x8408;
int lg=9;
long Frame [] = {0x0B,0x20,0x0B,0x19,0x10,0x30,0x05,0x27,0x01};
long Crc;
long carry;
Crc=0;
for (int j=0;j<lg;j++) {
Crc=Crc^Frame[j];
for (int i_bits=0 ; i_bits < lg-1 ; i_bits++ )
{
carry = Crc & 1;
Crc = Crc / 2;
if ( carry )
{
Crc = Crc ^ Poly;
}
}
}
System.out.println("CRC = " + Crc);
}
}
Twinuts
Messages postés5375Date d'inscriptiondimanche 4 mai 2003StatutModérateurDernière intervention14 juin 2023111 8 janv. 2012 à 16:56
Salut,
Contrairement au C en java il n'y a que les boolean qui peuvent s’écrire de manière simplifié.
Pour tout autre type (Object inclue) tu dois préciser l’égalité.
Dans ton cas tu peux soit changer le if(carry) en if(carry != 0)
soit changer le type de carry en boolean et du coup changer l'affectation