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 : 17458
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} CONVERSION DE LA BASE 2 À LA BASE 10
Ce petit code convertit les nombres décimaux en base 2 à la base 10 et réciproquement de la base 2 à...

{Visual Basic, VB6, VB.NET, VB 2005} TEXTURE D'IMAGE SUR UNE IMAGE
Appliquer dans un rectangle ou une ellipse une image en noir et blanc, sépia, couleurs inversées et ...

{Visual Basic, VB6, VB.NET, VB 2005} CONVERSION EURO EN FRANC FRANÇAIS
Ce logiciel permet de convertir les Euros en Francs Français. Il permet de convertir aussi les Fran...

{JAVA / J2EE} CONVERTISSEUR DE TEMPÉRATURE DEGRÉ CELCIUS - DEGRÉ FAHRENHEIT
Il convertir et ce en ligne de comande les températures de degrée celcius en degrée fahrenheit et vi...

{Visual Basic, VB6, VB.NET, VB 2005} CONVERSION DE LONGUEURS AVEC MULTIPLICATEUR
Ce logiciel permet de convertir des longueurs linéaires entre elles Il faut d'abord indiquer la q...

{Visual Basic, VB6, VB.NET, VB 2005} PROGRESSBAR PERSO
Créer un ProgressBar à l'aide de PictureBox. C'est un ProgressBar de style continuous qui s'utilise ...

{Visual Basic, VB6, VB.NET, VB 2005} CONVERSION DE LA BASE 1 À 36 EN DÉCIMAL ET RÉCIPROQUEMENT
Ce petit code ecrit en VB.Net 2003 permet de convertir les nombres tapés en base de 1 à 36 selon ce...

{Visual Basic, VB6, VB.NET, VB 2005} CSVTXTVERSDXF
L'objectif de ce programme est de convertir un fichier de données de type N°;X;Y;Z ou N°;X;Y en un d...