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 :

Générer matrice de binaires


Sujet :

Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre confirmé
    Homme Profil pro
    .
    Inscrit en
    Mai 2012
    Messages
    120
    Détails du profil
    Informations personnelles :
    Sexe : Homme

    Informations professionnelles :
    Activité : .

    Informations forums :
    Inscription : Mai 2012
    Messages : 120
    Par défaut Générer matrice de binaires
    Bonjour tout le monde
    je souhaite écrire un programme java me permettant de générer une matrice de binaire à partir d'une String donnée
    et par la même occasion ajouter un bit a la fin de chaque ligne et colonne.
    J'ai du mal a le faire
    Merci

  2. #2
    Membre chevronné
    Avatar de Laine
    Femme Profil pro
    Doctorat informatique
    Inscrit en
    Mars 2013
    Messages
    238
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Âge : 35
    Localisation : Algérie

    Informations professionnelles :
    Activité : Doctorat informatique

    Informations forums :
    Inscription : Mars 2013
    Messages : 238
    Par défaut
    Salut
    Ce serait bien si tu pouvais donner un début de code et expliquer exactement ou tu bloques s'il te plait

  3. #3
    Membre confirmé
    Homme Profil pro
    .
    Inscrit en
    Mai 2012
    Messages
    120
    Détails du profil
    Informations personnelles :
    Sexe : Homme

    Informations professionnelles :
    Activité : .

    Informations forums :
    Inscription : Mai 2012
    Messages : 120
    Par défaut
    Citation Envoyé par Laine Voir le message
    Salut
    Ce serait bien si tu pouvais donner un début de code et expliquer exactement ou tu bloques s'il te plait
    voici ce que j'ai essayé de faire:
    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
     
    public class MAT {
     
     
    	public static String toBinary(char c) {
     
    		String s = Integer.toBinaryString(c);
     
    		int n = Integer.bitCount(c);
    		if (n % 2 == 0) {
    			return s + '0';
    		} else {
    			return s  + '1';
    		}
    	}
    	public static char[] tochar(String str)
    	{
    		char []tab;
    		tab=new char[str.length()];
    		for(int i=0;i<str.length();i++)
    		{
    			tab[i]=str.charAt(i);
    		}
    		return tab;
    	}
    	public static void main(String[] args) {
    		String maChaine = "HELLOWORLD";	
    		char [] tab;
    		char [][]mat;
    		for (int i = 0; i < maChaine.length(); i++) {
    			String s = Integer.toBinaryString(maChaine.charAt(i));
    			s = toBinary(maChaine.charAt(i));
    			tab=new char [s.length()];
    			tab=tochar(s);
    			mat=new char[s.length()][8];
    			for(int j=0;j<8;j++)
    			{
    				mat[i]=tab;
    			}
    		System.out.println(mat[i]);
    		}
    à partir de ce code je voudrais générer une matrice a partir d'une String donnée par la suite ajouter un bit a la fin de chaque ligne et chaque colonne
    Merci a vous tout en espérant été clair

  4. #4
    Membre très actif

    Homme Profil pro
    Étudiant
    Inscrit en
    Août 2011
    Messages
    181
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Algérie

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Électronique et micro-électronique

    Informations forums :
    Inscription : Août 2011
    Messages : 181
    Billets dans le blog
    1
    Par défaut
    Slt.
    Tu pourrais commencer d'abord par convertir ton String en un tableau de int [], chaque case du tableau correspondra a la valeur d'un caractère de la chaine. En suite faire des et logiques pour obtenir la valeur de chacune des bit de l'octect :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    public static byte[] toByteArray (char[] array) {//Conversion char [] en byte []
    		return toByteArray(array, Charset.defaultCharset());
        }//toByteArray
        /**
         * 
         * @param array
         * @param charset
         * @return 
         */
        public static byte[] toByteArray (char[] array, Charset c){//toByteArray
    		CharBuffer cbuf = CharBuffer.wrap(array);
    		ByteBuffer bbuf = charset.encode(cbuf);
    		return bbuf.array();
        }//toByteArray
    Pour le test bit par bit :
    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
    String s = "Java";
    int [] tab = arrayByteToArrayInt (toByteArray (s.toCharArray ())));
     tab [0] = tab [0] & 0x01;
            if (tab [0] > 0)
                System.out.println ("1");
            else
                System.out.println ("0");
            tab [0] = tab [0] & 0x02;
            if (tab [0] > 0)
                System.out.println ("1");
            else
                System.out.println ("0");
    ...
    ...
    ...
    Pour la conversion byte->int, une simple affectation suffira (il va y avoir un cast).
    Bon courage

  5. #5
    Membre chevronné
    Avatar de Laine
    Femme Profil pro
    Doctorat informatique
    Inscrit en
    Mars 2013
    Messages
    238
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Âge : 35
    Localisation : Algérie

    Informations professionnelles :
    Activité : Doctorat informatique

    Informations forums :
    Inscription : Mars 2013
    Messages : 238
    Par défaut
    Si ceci est le résultat que tu veux obtenir:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    H: [1, 0, 0, 1, 0, 0, 0, 0]
    E: [1, 0, 0, 0, 1, 0, 1, 1]
    L: [1, 0, 0, 1, 1, 0, 0, 1]
    L: [1, 0, 0, 1, 1, 0, 0, 1]
    O: [1, 0, 0, 1, 1, 1, 1, 1]
    W: [1, 0, 1, 0, 1, 1, 1, 1]
    O: [1, 0, 0, 1, 1, 1, 1, 1]
    R: [1, 0, 1, 0, 0, 1, 0, 1]
    L: [1, 0, 0, 1, 1, 0, 0, 1]
    D: [1, 0, 0, 0, 1, 0, 0, 0]
    Voici ton code que j'ai quelque peu modifié
    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
    import java.util.Arrays;
     
    public class MAT {
     
    	public static String toBinary(char c) {
     
    		String s= Integer.toBinaryString(c);
    		int n= Integer.bitCount(c);
     
    		if (n % 2 == 0)
    			return s+"0";
    		else
        		return s+"1";
        }
     
        public static void main(String[] args) {
     
        	String maChaine= "HELLOWORLD";
        	char[][] mat= new char[maChaine.length()][8];
        	String s;
        	char c;
     
        	for(int i=0; i<maChaine.length(); i++) {
        		c= maChaine.charAt(i);
        		s= toBinary(c);
        		mat[i]= s.toCharArray();
     
        		System.out.println(c+": "+Arrays.toString(mat[i]));
        	}
        }
    }
    Pas besoin d'utiliser ta méthode "tochar", il y'a déjà une fonction pour ça: machaine.toCharArray().
    Sinon les autres modifications sont des modifications d'affectations.

  6. #6
    Membre confirmé
    Homme Profil pro
    .
    Inscrit en
    Mai 2012
    Messages
    120
    Détails du profil
    Informations personnelles :
    Sexe : Homme

    Informations professionnelles :
    Activité : .

    Informations forums :
    Inscription : Mai 2012
    Messages : 120
    Par défaut
    Citation Envoyé par Laine Voir le message
    Si ceci est le résultat que tu veux obtenir:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    H: [1, 0, 0, 1, 0, 0, 0, 0]
    E: [1, 0, 0, 0, 1, 0, 1, 1]
    L: [1, 0, 0, 1, 1, 0, 0, 1]
    L: [1, 0, 0, 1, 1, 0, 0, 1]
    O: [1, 0, 0, 1, 1, 1, 1, 1]
    W: [1, 0, 1, 0, 1, 1, 1, 1]
    O: [1, 0, 0, 1, 1, 1, 1, 1]
    R: [1, 0, 1, 0, 0, 1, 0, 1]
    L: [1, 0, 0, 1, 1, 0, 0, 1]
    D: [1, 0, 0, 0, 1, 0, 0, 0]
    Voici ton code que j'ai quelque peu modifié
    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
    import java.util.Arrays;
     
    public class MAT {
     
    	public static String toBinary(char c) {
     
    		String s= Integer.toBinaryString(c);
    		int n= Integer.bitCount(c);
     
    		if (n % 2 == 0)
    			return s+"0";
    		else
        		return s+"1";
        }
     
        public static void main(String[] args) {
     
        	String maChaine= "HELLOWORLD";
        	char[][] mat= new char[maChaine.length()][8];
        	String s;
        	char c;
     
        	for(int i=0; i<maChaine.length(); i++) {
        		c= maChaine.charAt(i);
        		s= toBinary(c);
        		mat[i]= s.toCharArray();
     
        		System.out.println(c+": "+Arrays.toString(mat[i]));
        	}
        }
    }
    Pas besoin d'utiliser ta méthode "tochar", il y'a déjà une fonction pour ça: machaine.toCharArray().
    Sinon les autres modifications sont des modifications d'affectations.

    Merci à vous tous lainec est effectivement ce que je cherchais
    mais est ce qu'il est possible d'ignorer les caractères spéciaux

  7. #7
    Membre confirmé
    Homme Profil pro
    .
    Inscrit en
    Mai 2012
    Messages
    120
    Détails du profil
    Informations personnelles :
    Sexe : Homme

    Informations professionnelles :
    Activité : .

    Informations forums :
    Inscription : Mai 2012
    Messages : 120
    Par défaut
    Citation Envoyé par mohamine1989 Voir le message
    Slt.
    Tu pourrais commencer d'abord par convertir ton String en un tableau de int [], chaque case du tableau correspondra a la valeur d'un caractère de la chaine. En suite faire des et logiques pour obtenir la valeur de chacune des bit de l'octect :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    public static byte[] toByteArray (char[] array) {//Conversion char [] en byte []
    		return toByteArray(array, Charset.defaultCharset());
        }//toByteArray
        /**
         * 
         * @param array
         * @param charset
         * @return 
         */
        public static byte[] toByteArray (char[] array, Charset c){//toByteArray
    		CharBuffer cbuf = CharBuffer.wrap(array);
    		ByteBuffer bbuf = charset.encode(cbuf);
    		return bbuf.array();
        }//toByteArray
    Pour le test bit par bit :
    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
    String s = "Java";
    int [] tab = arrayByteToArrayInt (toByteArray (s.toCharArray ())));
     tab [0] = tab [0] & 0x01;
            if (tab [0] > 0)
                System.out.println ("1");
            else
                System.out.println ("0");
            tab [0] = tab [0] & 0x02;
            if (tab [0] > 0)
                System.out.println ("1");
            else
                System.out.println ("0");
    ...
    ...
    ...
    Pour la conversion byte->int, une simple affectation suffira (il va y avoir un cast).
    Bon courage
    Merci bien mais à vue d’œil le code sera long

  8. #8
    Membre chevronné
    Avatar de Laine
    Femme Profil pro
    Doctorat informatique
    Inscrit en
    Mars 2013
    Messages
    238
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Âge : 35
    Localisation : Algérie

    Informations professionnelles :
    Activité : Doctorat informatique

    Informations forums :
    Inscription : Mars 2013
    Messages : 238
    Par défaut
    mais est ce qu'il est possible d'ignorer les caractères spéciaux
    Oui bien sûr, ce code pourrait t'aider:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    public boolean isLetter(char ch) {
         return Character.toString(ch).matches("[a-zA-Z]");
    }
    Cette fonction permet de tester si un caractère est une lettre inclue entre a et z, en majscule et minuscule.

    Et si tu veux inclure les accents aussi, utilise:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    Character.isLetter(char);
    Dans ce cas la taille de ton tableau mat ne sera plus la taille de ta chaîne String, tu peux laisser ton code ainsi ce n'est pas vraiment un problème ou tu peux utiliser une ArrayList pour mat. Mais ce n'est pas obligatoire.

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Générer des fichiers binaires
    Par scolyo dans le forum Langage
    Réponses: 4
    Dernier message: 04/11/2010, 09h10
  2. Générer un vecteur binaire 8bits aléatoire
    Par mirva dans le forum MATLAB
    Réponses: 2
    Dernier message: 16/12/2008, 16h47
  3. Générer des séquences binaires
    Par Contrec dans le forum C#
    Réponses: 16
    Dernier message: 03/12/2008, 15h46
  4. générer un fichier binaire OCTET par OCTET
    Par Septembre84 dans le forum Entrée/Sortie
    Réponses: 11
    Dernier message: 20/03/2008, 16h07

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