Les Snippets

Connexion

Tableau de bytes vers tableau de booléens

Niveau requis pour utiliser/comprendre cette source : 2 ( Initié )
Créé le 29/10/2007 15:52:27 et initié par the_wwt [Liste]
Date de mise à jour : 09/04/2008 17:26:16
Vue : 6575
Catégorie(s) : Compression & Split
Langages dispo pour ce code :
- Java
- Delphi 5
- Caml, CamlLight, ObjectiveCaml
- VB6, VBA



Langage : Java
Date ajout : 29/10/2007
Posté par the_wwt [Liste]
   /**
     * Convert a bytes array to booleans array.
     * 
     * @param bytes
     *            The array to convert.
     * @return A booleans array from bytes array.
     */
    private boolean[] bytes2booleans(byte[] bytes) {
        boolean[] booleans = new boolean[bytes.length * 8];
        for (int iByte = 0; iByte < bytes.length; iByte++) {
            for (int iBool = 1; iBool < 8; iBool++)
                booleans[iByte * 8 + iBool] = (bytes[iByte] & (byte) Math.pow(
                        2, 8 - iBool - 1)) > 0 ? true : false;
            if (bytes[iByte] < 0)
                booleans[iByte * 8] = true;
            else
                booleans[iByte * 8] = false;
        }
        return booleans;
    }
Langage : Delphi 5
Date ajout : 09/04/2008
Posté par f0xi [Liste]
DateMAJ : 09/04/2008
 
 Type
   TBoolBits = array[0..7] of boolean; 
   TBoolBitsArray = array of TBoolBits; 
   TByteArray = array of byte; 
 
 Procedure BytesToBoolBits(const ByteArray: TByteArray; 
 var BoolBitsArray : TBoolBitsArray); 
 var i, L: integer; 
 begin 
   L := Length(ByteArray); 
   SetLength(BoolBitsArray, L); 
   for I := 0 to L-1 do 
   begin 
     BoolBitsArray[I,7] := (ByteArray[I] and $80) = $80; 
     BoolBitsArray[I,6] := (ByteArray[I] and $40) = $40; 
     BoolBitsArray[I,5] := (ByteArray[I] and $20) = $20; 
     BoolBitsArray[I,4] := (ByteArray[I] and $10) = $10; 
     BoolBitsArray[I,3] := (ByteArray[I] and $08) = $08; 
     BoolBitsArray[I,2] := (ByteArray[I] and $04) = $04; 
     BoolBitsArray[I,1] := (ByteArray[I] and $02) = $02; 
     BoolBitsArray[I,0] := (ByteArray[I] and $01) = $01; 
   end; 
 end;
 
                           
Langage : Caml , ObjectiveCaml , CamlLight
Date ajout : 09/04/2008
Posté par coucou747 [Liste]
let rec byteListAdd nbrBits acc i = if nbrBits = 0 then acc else byteListAdd (nbrBits-1) ( (i mod 2 = 1) ::acc) (i / 2);;
let formStringToBinarySeq str =
    let strget=String.get str
    in let rec f acc i = if i = -1
        then acc
        else f (byteListAdd 8 acc (int_of_char (strget i) )) (i-1)
    in f [] ((String.length str) -1);;
Remarque :
(*reciproque et test*)
let formBinarySeqToString tab =
let rec f l i c = function
| [] ->String.concat "" (List.rev ((String.make 1 (char_of_int c))::l))
| hd::tl ->
if i=8 then f ((String.make 1 (char_of_int c))::l) 0 0 (hd::tl)
else f l (i+1) (c*2 + (if hd then 1 else 0)) tl
in f [] 0 0 tab;;

formBinarySeqToString (formStringToBinarySeq "moi");;
Langage : VB6 , VBA
Date ajout : 06/11/2008
Posté par jrivet [Liste]
'je n'ai pas utilisé Lbound() mais rien ne l'empêche
'Passer donc un tableau de base 0 (ou la modifier :) )
Private Function Bytes2Bools(Bytes() As Byte, MultiDim As BooleanAs Boolean()
Dim iByte  As Integer
Dim iBool As Integer
Dim Res() As Boolean
    'on dimensione le tableau de sortie
    If MultiDim Then
        ReDim Res(0 To UBound(Bytes), 0 To 7)
    Else
        ReDim Res(0 To ((UBound(Bytes) + 1* 8- 1)
    End If
    'pour chaque Byte contenu dans
    'le tableau des parametre
    For iByte = 0 To UBound(Bytes)
        'pour chaque bit du Byte
        For iBool = 0 To 7
            'suivant le choix de la dimension
            If MultiDim Then
                Res(iByte, iBool) = (((2 ^ iBool) And Bytes(iByte)) <> 0)
            Else
                Res((iByte * 8+ iBool) = (((2 ^ iBool) And Bytes(iByte)) <> 0)
            End If
        Next iBool
    Next iByte
    'on retourne le tableau construit
    Bytes2Bools = Res
End Function
Remarque :
Petite version VBA/VB6. La encore cela m'étonne qu'elle ne soit pas déjà présente.

Snippets en rapport avec : Conversion, Bits, Booléens, Bytes, Binaires



Codes sources en rapport avec : Conversion, Bits, Booléens, Bytes, Binaires

{Python} CONVERSION ET COMPARAISON D'ENTIER RELATIFS EN BINAIRE
Slt! J'ai écri une fonction qui permet de convertir à la fois deux entiers relatifs en binaire sur k...

{Visual Basic, VB6, VB.NET, VB 2005} TRADUCTEUR VB6.VBP EN VB5.VBP
Ben oui ! Il y a encore des utilisateurs de VB5. C'est ce qui m'a amené à faire ce petit programm...

{Visual Basic, VB6, VB.NET, VB 2005} EXPORT TABLEAU EXCEL EN BBCODE
Petite source en vba excel permettant d'exporter le tableau Excel en BBcode (tableau basic, seul l'é...

{Visual Basic, VB6, VB.NET, VB 2005} HEXA/ASCII AVEC POSSIBILITÉ DE FICHIER
Salut a tous ! (svp prenez la peine de lire la description jusqu'au bout please!!) voila c'est 1 ma...

{JAVA / J2EE} FLV TO MP3
Cette source permet d'extraire un mp3 d'un fichier flv. Je suis à la recherche de testeurs. C'est...

{C / C++ / C++.NET} ITOA FAIT MASION: COVERSION ENTIER 32 BIT NON SIGNE VERS CHAINE DE CARACTERE(UNIX/WIN32)
permet la conversion d'un entier non signe 32 bits vers une une chaine de caractère ; la fonction es...

{Python} CONVERTISSEUR DE SECONDES
Un petit programme qui convertit un nombre entier de secondes, fourni au départ, en un nombre d'anné...

{Visual Basic, VB6, VB.NET, VB 2005} VBSCRIPT --> CONVERSION DE FICHIER TEXTE EN FICHIER EXCEL PAR LIGNE DE COMMANDE
Ce script, lancé en ligne de commande dos, permet de convertir un fichier texte en fichier excel. ...

{Flash} CONVERTIR MILLISECONDES EN MINUTES:SECONDES. ( EN AS2 / DÉBUTANT )
Un simple script pour convertir une durée exprimée en millisecondes en minutes:secondes. Je me su...

{SQL} CONVERSION DE NOMBRE EN LETTRES (SQL)
Je me suis inspiré du code c# trouvé ici (http://files.codes-sources.com/fichier.aspx?id=21491&f=con...