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 : 15169
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} TRIEUR DE PHOTOS
Ce programme permet de sélectionner des photos et de les enregistrer dans un dossier de destination....

{C / C++ / C++.NET} CONVERSION DE FICHIER EN FICHIER BMP
Dans la même optique que ma source précédente, voici un programme pour faire des fichiers bmp en uti...

{Visual Basic, VB6, VB.NET, VB 2005} CONVERTISSEUR HEXAVIGÉSIMAL
Ce convertisseur comme son nom l'indique permet d'effectuer l'incrémentation d'une combinaison de le...

{Assembleur} DUMPER HEXADÉCIMAL
Explorateur 'old style' de fichiers avec affichage en Hexadécimal (dump). La partie explorer est dan...

{Visual Basic, VB6, VB.NET, VB 2005} CONVERTIR DU TEXTE RTF EN CODE HTML ET VICE-VERSA
Bonjour, Je sais que certaines sources du genre existent sur le site, parfois volumineuses, mais en ...

{C / C++ / C++.NET} CONVHTML : UN UTILITAIRE DE CONVERSION POUR FICHIERS HTML
On peut créer ou modifier le fichier html d'une page web avec le Bloc-note, Wordpad ou un autre trai...

{Visual Basic, VB6, VB.NET, VB 2005} CONVERSION DE DEVISE MONAITAIRE VIA UN SERVICE WEB
Petite librairie permettant de récupérer les taux le conversion pour passer d'une devise à l'autre.....

{C / C++ / C++.NET} DATETIMECONVERTER
dateTimeConverter(1) User Commands ...