Ajouter un commentaire

thesuixx

tu peux utiliser le DataInputStream:

// l'encodage: sur 2 octets
public static final int NB_BYTES_ENCODAGE = 2;
// le flux       
in = new DataInputStream(new FileInputStream(fichierBinaire));
// le tableau servant à temporairement stocker les bytes
byte[] byteArray = new byte[NB_BYTES_ENCODAGE];

// lecture du fichier
while (in.read(byteArray) != -1)
         System.out.println(byteArrayToInt(byteArray));

et voici la fonction pour obtenir tes valeurs:

    /**
     * extrait la valeur int d'un byte array en big endian
     * @param byteArray: un byte array en ordre big endian
     * @return int
     */
    private int byteArrayToInt(byte[] byteArray) {
        int total = 0;
        for (int i = 0; i < byteArray.length; i++) {
            total += unsignedByteToInt(byteArray[i]) * (i == 0 ? 1 : i*256);
        }
        return total;
    }
    
    /**
     * effectue la conversion d'un byte en int en traitant le byte comme s'il était unsigned
     * @param b: le byte
     * @return la valeur int du byte comme s'il était unsigned
     */
    private int unsignedByteToInt(byte b){
        int res = (int)b;
        if (res < 0){
            res = 256 + res;  // voir complément à deux
        }
        return res;
    }

* si les valeurs sont signées, tu ne fais pas appel à unsignedbyteToInt mais tu prends la vraie valeur de l'octet

EDIT: me suis trompé, ca ne marche qu'avec un encodage sur 2 octets, si tu as un encodage sur 4 ou 8 tu permutes les octets 2 par 2 dans byteArrayToInt

Filtered HTML

Plain text

CAPTCHA
Cette question permet de vérifier que vous n'êtes pas un robot spammeur :-)
 L     U   U  DDD   Y   Y  V     V 
L U U D D Y Y V V
L U U D D Y V V
L U U D D Y V V
LLLL UUU DDD Y V