Java - Convertir chiffre en char

Lilou -  
 nader -
Bonjour,

Est ce que quelqu'un sait comment un chiffre en char ou en chaine en Java

En fait je fais une soustraction entre deux char, et ce que je récupère est un int.
Moi je voudrai retrouver la valeur du char correspondant.

Merci

Lilou

5 réponses

  1. choubaka Messages postés 5535 Date d'inscription   Statut Modérateur Dernière intervention   2 113
     
    Salut

    le plus simple est d'utiliser la méthode statique de "String", valueOf(int i), celle ci te renvoie la valeur en String de l'argument.

    Cette méthode fonctionne également avec d'autres types

    static String valueOf(boolean b) 
              Returns the string representation of the boolean argument. 
    static String valueOf(char c) 
              Returns the string representation of the char argument. 
    static String valueOf(char[] data) 
              Returns the string representation of the char array argument. 
    static String valueOf(char[] data, int offset, int count) 
              Returns the string representation of a specific subarray of the char array argument. 
    static String valueOf(double d) 
              Returns the string representation of the double argument. 
    static String valueOf(float f) 
              Returns the string representation of the float argument. 
    static String valueOf(int i) 
              Returns the string representation of the int argument. 
    static String valueOf(long l) 
              Returns the string representation of the long argument. 
    static String valueOf(Object obj) 
              Returns the string representation of the Object argument. 
    


    1
    1. lilou
       
      merci de ta réponse

      j'avais déjà essayé mais ca ne marche pas . en fait c peut etre mes chiffres de départ qui ne sont pas bons.

      En fait je fais :

      char m = 'm' ;
      char a = 'a' ;

      int i = m ^ a ;

      puis

      int res = m ^i ;

      et a partir de la je voudrai retrouver a ;

      est ce possible ?

      peut on faire un ou exclusiif entre int et char ?
      0
      1. choubaka Messages postés 5535 Date d'inscription   Statut Modérateur Dernière intervention   2 113 > lilou
         
        salut

        oui c'est possible, mais tu ne peux pas utiliser des primitifs mais les objets correspondants ...

        je te prépare un exemple, wait 2 secondes
        0
      2. lilou > choubaka Messages postés 5535 Date d'inscription   Statut Modérateur Dernière intervention  
         
        merci

        j'attend . :-)
        0
      3. choubaka Messages postés 5535 Date d'inscription   Statut Modérateur Dernière intervention   2 113 > lilou
         
        Tu dois utiliser des objets string au lieu des "char", quitte à les convertir.

        char m = 'm';
        char a = 'a';
        
        int tempoM = Integer.getInteger(String.valueOf(m)).intValue();
        int tempoA = Integer.getInteger(String.valueOf(a)).intValue();
        
        int i = tempoM ^ tempoA ; 
        
        int res =TempoM ^i ; 
        
        
        0
      4. choubaka Messages postés 5535 Date d'inscription   Statut Modérateur Dernière intervention   2 113 > lilou
         
        je vais décomposer l'histoire

        char m = 'm';
        char a = 'a';
        
        String stringM = String.valueOf(m);
        String stringA= String.valueOf(a);
        
        Integer  integerM = Integer.getInteger(stringM);
        Integer  integerA = Integer.getInteger(stringA);
        
        int intM = integerM.intValue();
        int inta = integerA.intValue();
        
        int i = intM ^ intA ; 
        
        int res =intM ^i ; 
        
        
        


        c'est exactement la même chose mais décomposé phase par phase
        0
  2. dnt91 Messages postés 48 Statut Membre 41
     
    Salut, j'ai pas trés bien compri ce que tu voulais faire, mais en règle générale, pour convertir un int en string, ya la méthode toString(),
    int c = 2;
    string chaine = c.toString();
    Ce code met "2" dans la variable chaine.
    Maintenant, si tu fais une différence entre 2char, je ne sais plus quelle méthode permet de récupérer le code ascii correspondant.
    0
  3. lilou
     
    MERCI :-)

    j'ai réussi grace a toi à faire ce que je voulais :-)

    a+

    lilou
    0
  4. choubaka Messages postés 5535 Date d'inscription   Statut Modérateur Dernière intervention   2 113
     
    à vot'service ...

    -1
  5. Vous n’avez pas trouvé la réponse que vous recherchez ?

    Posez votre question
  6. nader
     
    le char peut etre un int ou un string mais pas l'inverse si on pense ou String;
    exemple;
    char a='A';
    int c=a+1; //c=65 car dans cette cas le a=64, 'A'=64,'B'=65,..;
    donc si on fait
    char x=c;// donc x est le caractère 'B'
    enfin pour convertir le int ou le char en String tu peut faire le casting exemple;
    String x=""+c/.........
    0