IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Java Discussion :

masque et decallage


Sujet :

Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre chevronné
    Profil pro
    Inscrit en
    Septembre 2009
    Messages
    1 855
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2009
    Messages : 1 855
    Par défaut masque et decallage
    bonjour,

    voici mon code :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    			long val = (long)(valInt & 0xFF);
     
    			System.out.println("1- crcTmp: " + String.format("%08X", crcTmp)); // debug
     
    			long val1 = (crcTmp << 8) & 0xFFFFFFFF;
    			System.out.println("2a- val1: " + String.format("%08X", val1)); // debug
    je ne comprends pas pourquoi dans certains cas, ça m'affiche :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    1- crcTmp: FF0000FF0810701F
    2a- val1: FF0810701F00
    => comment val1 peut-il avoir une valeur supérieure à 0xFFFFFFFF alors que je fais un masque (0xFFFFFFFF) ?

    merci d'avance

  2. #2
    Expert éminent
    Avatar de tchize_
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Avril 2007
    Messages
    25 482
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 46
    Localisation : Belgique

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Avril 2007
    Messages : 25 482
    Par défaut
    si je ne me trompe pas, 0xFFFFFFFF est int (32 bits), qui vaut -1, comme tu applique ce masque à un Long, il est étendu (conversion int -> long). Cette conversion préserve la valeur, tu obtiens donc -1 en Long, c'est à dire 0xFFFFFFFFFFFFFFFF et c'est ce masque qui est appliqué.

    Donc change ton code comme ceci devrais faire l'affaire:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    long val = (long)(valInt & 0xFFL);
     
    			System.out.println("1- crcTmp: " + String.format("%08X", crcTmp)); // debug
     
    			long val1 = (crcTmp << 8) & 0xFFFFFFFFL;
    			System.out.println("2a- val1: " + String.format("%08X", val1)); // debug

  3. #3
    Membre Expert Avatar de jabbounet
    Homme Profil pro
    Consultant informatique
    Inscrit en
    Juin 2009
    Messages
    1 909
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 49

    Informations professionnelles :
    Activité : Consultant informatique

    Informations forums :
    Inscription : Juin 2009
    Messages : 1 909
    Par défaut
    je crois qu'il veut appliquer un masque pour limiter sa valeur sur 32 bits.

    Dans ce cas son masque serait plutôt: 0x00000000FFFFFFFF;

    Je ne sais pas si java propose un notation plus courte pour ce cas.

  4. #4
    Expert éminent
    Avatar de tchize_
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Avril 2007
    Messages
    25 482
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 46
    Localisation : Belgique

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Avril 2007
    Messages : 25 482
    Par défaut
    Citation Envoyé par jabbounet Voir le message
    Dans ce cas son masque serait plutôt: 0x00000000FFFFFFFF;
    Ce qui est exactement ce que j'ai mis si je ne me trompe

  5. #5
    Membre chevronné
    Profil pro
    Inscrit en
    Septembre 2009
    Messages
    1 855
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2009
    Messages : 1 855
    Par défaut
    merci tchize.

    En faite j'ai besoin de faire ça pour me faire une classe de calcul de CRC

    le code C est :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
     
    #define u_long unsigned long 
     #define u_char unsigned char 
     
     u_long crc32(u_char *, int); 
     u_long crc32_table[256]; 
     
     int main(void) 
     { 
         u_char text[10]; 
         u_long crc;  
         int n; 
     
         while (1) 
         { 
             printf("\nInput string: "); 
             scanf("%s", text); 
             if (!strcmp(text, "999")) 
                 break; 
             crc = crc32(text, 4); 
             printf("CRC = %lX\n", crc); 
         } 
     
     } 
     
     
     /* Initialized first time "crc32()" is called. If you prefer, you can 
      * statically initialize it at compile time. [Another exercise.] 
      */ 
     
     u_long crc32(u_char *buf, int len) 
     { 
             u_char *p; 
             u_long  crc; 
     
             if (!crc32_table[1])    /* if not already done, */ 
                     init_crc32();   /* build table */ 
             crc = 0xffffffff;       /* preload shift register, per CRC-32 spec */ 
             for (p = buf; len > 0; ++p, --len) 
                     crc = (crc << 8) ^ crc32_table[(crc >> 24) ^ *p]; 
             return ~crc;            /* transmit complement, per CRC-32 spec */ 
     } 
     
     /* 
      * Build auxiliary table for parallel byte-at-a-time CRC-32. 
      */ 
     #define CRC32_POLY 0x04c11db7     /* AUTODIN II, Ethernet, & FDDI 0x04C11DB7 */ 
     
     init_crc32() 
     { 
             int i, j; 
             u_long c; 
     
             for (i = 0; i < 256; ++i) { 
                     for (c = i << 24, j = 8; j > 0; --j) 
                             c = c & 0x80000000 ? (c << 1) ^ CRC32_POLY : (c << 1); 
                     crc32_table[i] = c; 
             } 
     }

    avec mes quelques connaissance en java, j'ai créé cette classe :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
     
    /**
            Calcul de CRC32
     */
    public class ClassCRC32 {
     
    	private long pcrc;
    	private long[] ptabConst = new long[256];
    	private long DefaultPoly = StaticDefaultPoly;
     
     
    	final private static long StaticDefaultPoly = 0x04c11db7L; // AUTODIN II, Ethernet, FDDI
     
     
    	//*******************************************
    	public ClassCRC32(){
    		pcrc = initCRC(ptabConst, DefaultPoly);
    	}
     
    	public ClassCRC32(long crc32_poly){
    		pcrc = initCRC(ptabConst, crc32_poly);
    	}
     
     
    	/**
             * @param tab : tableau de byte a tester
             */
    	public ClassCRC32(int[] tab){
    		pcrc = initCRC(ptabConst, DefaultPoly);
    		add(tab);
    	}
     
    	/**
             * 
             * @param tab : tableau de byte a tester
             * @param crc_poly : valeur définissant la complexité de la focntion CRC polynominal
             */
    	public ClassCRC32(int[] tab, long crc32_poly){
    		pcrc = initCRC(ptabConst, crc32_poly);
    		add(tab);
    	}
     
     
    	//*******************************************
    	/**
             * @param tab : tableau de byte a tester
             */
    	public void add(int[] tab){
    		pcrc = addData(pcrc, tab, ptabConst);
    	}
     
     
    	//*******************************************
    	public long getCRC(){
    		return getVal(pcrc);
    	}
     
     
     
     
     
    	// **************************************************************	
    	// **************************************************************
    	private static long initCRC(long[] tabCRC, long poly){
    		long i, j; 
    		long c; 
     
    		for (i = 0; i < 256; ++i){ 
    			for (c = i << 24, j = 8; j > 0; --j) {
    				if((c & 0x80000000L) != 0){
    					c = (c << 1) ^ poly;
    				} else {
    					c = (c << 1);
    				}
    			}
     
    			tabCRC[(int)i] = c & 0xFFFFFFFFL; 
    		}
     
    		return 0xffffffffL;
    	} 
     
     
    	private static long addData(long crc, int[] tab, long[] tabCRC){
    		long crcTmp = crc;
     
     
    		for(int valInt : tab){
     
    			long val = (long)(valInt & 0xFF); // on ne traite que 8 bits (utilisation de int pour éviter les problème de signes)
     
    			System.out.println("1- crcTmp: " + String.format("%08X", crcTmp)); // debug
     
    			long val1 = (crcTmp << 8) & 0xFFFFFFFFL;
    			System.out.println("2a- val1: " + String.format("%08X", val1)); // debug
     
    			long val2 = ((crcTmp & 0xFFFFFFFFL) >>> 24);
    			System.out.println("3- val2: " + String.format("%08X", val2)); // debug
     
    			val2 ^= val;
    			System.out.println("4- val2: " + String.format("%08X", val2) + " / val: " + String.format("%08X", val)); // debug
     
    			val2 = tabCRC[(int)(val2 & 0xFF)];
    			System.out.println("5- val2: " + String.format("%08X", val2)); // debug
     
    			crcTmp = val1 ^ val2;
    			System.out.println("6- crcTmp: " + String.format("%08X", crcTmp)); // debug
     
    			//crcTmp = (crcTmp << 8) ^ tabCRC[(crcTmp >> 24) ^ val];
    		}
     
    		return crcTmp;
    	} 
     
    	private static long getVal(long crc){
    		return (~crc) & 0xFFFFFFFFL;
    	}
     
     
    	// *******************************************************
    	// *******************************************************
     
    	/**
             * Récupération du CRC avec paramêtrage de la formule polynomiale
             */
    	static public long getCRC(int[] tab, long poly){
    		long[] tabConst = new long[256];
    		long crc;
     
    		crc = initCRC(tabConst, poly);
    		crc = addData(crc, tab, tabConst);
    		crc = getVal(crc);
     
    		return crc;
    	}
     
    	/**
             * Récupération du CRC
             */
    	static public long getCRC(int[] tab){
    		long[] tabConst = new long[256];
    		long crc;
     
    		crc = initCRC(tabConst, StaticDefaultPoly);
    		crc = addData(crc, tab, tabConst);
     
    		return getVal(crc);
    	}
     
     
     
    	// **************************************************************	
    	// **************************************************************
    	/**
             * Récupération du CRC en chaine de caractères
             */
    	public String toString(){
    		return String.format("%08X", getVal(pcrc));
    	}
    }
    => vous voyez des boulettes ? comment puis-je vérifier que le calcul est bon ?

  6. #6
    Membre chevronné
    Profil pro
    Inscrit en
    Septembre 2009
    Messages
    1 855
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2009
    Messages : 1 855
    Par défaut
    J'ai testé mon code avec cet exemple :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
     
            String str = "123456789";
            char[] tab = str.toCharArray();
            int[] tabint = new int[tab.length];
     
            for (int i=0; i<tab.length;i++){
            	tabint[i] = tab[i];
            }
     
            ClassCRC32 crc = new ClassCRC32(tabint);
            System.out.println("CRC : " + crc.toString());
    Mais ça ne me donne pas le même résultat que sur ce site : http://www.lammertbies.nl/comm/info/...lculation.html

  7. #7
    Membre Expert Avatar de jabbounet
    Homme Profil pro
    Consultant informatique
    Inscrit en
    Juin 2009
    Messages
    1 909
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 49

    Informations professionnelles :
    Activité : Consultant informatique

    Informations forums :
    Inscription : Juin 2009
    Messages : 1 909
    Par défaut
    Citation Envoyé par boboss123 Voir le message
    J'ai testé mon code avec cet exemple :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
     
            String str = "123456789";
            char[] tab = str.toCharArray();
            int[] tabint = new int[tab.length];
     
            for (int i=0; i<tab.length;i++){
            	tabint[i] = tab[i];
            }
     
            ClassCRC32 crc = new ClassCRC32(tabint);
            System.out.println("CRC : " + crc.toString());
    Mais ça ne me donne pas le même résultat que sur ce site : http://www.lammertbies.nl/comm/info/...lculation.html
    Peut-être a cause de la façon de convertir ta string en tableau de int.

    Ici chaque int de ton tableau va contenir une valeur que peut contenir un char c'est a dire entre 0 et 65535.

    A priori tu peux essayer avec Interger.parsInt()
    ou si la valeur est trop grande, convertir ton string en tableau de byte et t'arranger pour mettre 4 byte dans un int.

Discussions similaires

  1. [JtextField]Creer un masque pour Ip
    Par bibx dans le forum Composants
    Réponses: 8
    Dernier message: 11/01/2005, 17h31
  2. Masque vidéo
    Par pifou02 dans le forum Composants VCL
    Réponses: 6
    Dernier message: 24/09/2003, 08h09
  3. Réponses: 7
    Dernier message: 17/07/2003, 11h00
  4. flashMX pb avec les masques
    Par annsoo dans le forum Flash
    Réponses: 2
    Dernier message: 12/05/2003, 12h22
  5. Bas les masques
    Par benj63 dans le forum C++Builder
    Réponses: 8
    Dernier message: 03/07/2002, 17h05

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo