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

Codes sources à télécharger Java Discussion :

Convertir des nombres (hexadecimal, decimal, octal, binaires)


Sujet :

Codes sources à télécharger Java

  1. #1
    Membre averti

    Homme Profil pro
    Développeur informatique
    Inscrit en
    Juin 2011
    Messages
    122
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Madagascar

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Juin 2011
    Messages : 122
    Points : 327
    Points
    327
    Par défaut Convertir des nombres (hexadecimal, decimal, octal, binaires)
    Bonjour,

    Je vous propose un nouvel élément à utiliser : Convertir des nombres (hexadecimal, decimal, octal, binaires)

    Cette classe permet d'effectuer ces conversions (à double sens):


    • binaire decimal

      octal decimal

      hexadecimal decimal

      binaire octal

      binaire hexadecimal

      hexadecimal octal



    Qu'en pensez-vous ?

  2. #2
    Membre averti

    Homme Profil pro
    Développeur informatique
    Inscrit en
    Juin 2011
    Messages
    122
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Madagascar

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Juin 2011
    Messages : 122
    Points : 327
    Points
    327
    Par défaut
    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
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
     
    import java.util.ArrayList;
    import java.util.regex.Pattern;
     
    /**
     *
     * @author AASPRONI
     */
    public class convert {
        private ArrayList<Integer> resultat;
     
        public convert() {
            this.resultat = new ArrayList<Integer>();
        }
     
     
     
        public ArrayList<Integer> getResultat() {
            return resultat;
        }
     
        public void setResultat(ArrayList<Integer> resultat) {
            this.resultat = resultat;
        }
         /**
            * Méthode qui converti un nombre decimal en nombre binaire
            * @param arg l'entier à convertir
            * @return String, représentaion en caractère du résultat
            */
        public String DecimalToBinaire(int arg){
            try{
                int leng;
                String res="";
                this.resultat.clear();
                while(arg>1){
                    this.resultat.add(arg%2);
                    arg=(arg-(arg%2))/2;
                }
                this.resultat.add(arg);
                leng=this.resultat.size();
                for(int i=leng;i!=0;i--){
                    int a=this.resultat.get(i-1);
                    res=res+Integer.toString(a);
                }
                return res;
            }
            catch(NumberFormatException ex){
                return "ce n'est pas un chiffre decimal";
            }
        }
     
         /**
            * Méthode qui converti un nombre decimal en nombre ocal
            * @param arg l'entier à convertir
            * @return String, représentaion en caractère du résultat
            */
        public String DecimalToOctal(int arg){
            try{
                int leng;
                String res="";
                this.resultat.clear();
                while(arg>7){
                    this.resultat.add(arg%8);
                    arg=(arg-(arg%8))/8;
                }
                this.resultat.add(arg);
                leng=this.resultat.size();
                for(int i=leng;i!=0;i--){
                    int a=this.resultat.get(i-1);
                    res=res+Integer.toString(a);
                }
                return res;
            }
            catch(NumberFormatException ex){
                return "ce n'est pas un chiffre decimal";
            }
        }
     
         /**
            * Méthode qui converti un nombre decimal en nombre hexadecimal
            * @param arg l'entier à convertir
            * @return String, représentaion en caractère du résultat
            */
        public String DecimalToHexadecimal(int arg){
            try{
                int leng;
     
                String res="";
                this.resultat.clear();
                while(arg>15){
                    this.resultat.add(arg%16);
                    arg=(arg-(arg%16))/16;
                }
                this.resultat.add(arg);
                leng=this.resultat.size();
                for(int i=leng;i!=0;i--){
                    int a=this.resultat.get(i-1);
                    if(a==10)res=res+"A";
                    else if(a==11)res=res+"B";
                    else if(a==12)res=res+"C";
                    else if(a==13)res=res+"D";
                    else if(a==14)res=res+"E";
                    else if(a==15)res=res+"F";
                    else res=res+Integer.toString(a);
                }
                return res;
            }
            catch(NumberFormatException ex){
                return "ce n'est pas un chiffre decimal";
            }
        }
     
     
        public int power(int a,int p){
            int res=1;
            for(int i=0;i<p;i++)res*=a;
            return res;
        }
     
         /**
            * Méthode qui converti un nombre binaire en nombre decimal
            * @param arg le chiffre binaire à convertir
            * @return int, résultat en décimal
            */
        public int BinaireToDecimal(String arg){
            if (Pattern.matches("[0-1]*",arg)) {
                int leng=arg.length();
                int res=0;
                for(int i=leng;i!=0;i--){
                    int a=Integer.parseInt(Character.toString(arg.charAt(i-1)));
                    res+=(a*power(2,leng-i));
                }
                return res;
            }        
            else {
                 System.out.println("ce n'est pas un chiffre octal");
                return -1;
            } 
     
        }
     
     
        /**
           * Méthode qui converti un nombre octal en nombre decimal
           * @param arg le chiffre octal à convertir
           * @return int, résultat en décimal
           */
        public int OctalToDecimal(String arg){
            if (Pattern.matches("[0-7]*",arg)) {
                int leng=arg.length();
                int res=0;
                for(int i=leng;i!=0;i--){
                    int a=Integer.parseInt(Character.toString(arg.charAt(i-1)));
                    res+=(a*power(8,leng-i));
                }
                return res;
            }        
            else {
                 System.out.println("ce n'est pas un chiffre octal");
                return -1;
            }    
        }
     
         /**
            * Méthode qui converti un nombre hexadecimal en nombre decimal
            * @param arg le chiffre hexadecimal à convertir
            * @return int, résultat en décimal
            */
        public int HexadecimalToDecimal(String arg){
           if (Pattern.matches("([0-9]|[a-f]|[A-F])*",arg)) {           
            int leng=arg.length();
            int res=0;
            int a;
            for(int i=leng;i!=0;i--){
                if(arg.charAt(i-1)=='A' || arg.charAt(i-1)=='a')a=10;
                else if(arg.charAt(i-1)=='B' || arg.charAt(i-1)=='b')a=11;
                else if(arg.charAt(i-1)=='C' || arg.charAt(i-1)=='c')a=12;
                else if(arg.charAt(i-1)=='D' || arg.charAt(i-1)=='d')a=13;
                else if(arg.charAt(i-1)=='E' || arg.charAt(i-1)=='e')a=14;
                else if(arg.charAt(i-1)=='F' || arg.charAt(i-1)=='f')a=15;
                else a=Integer.parseInt(Character.toString(arg.charAt(i-1)));
                res+=(a*power(16,leng-i));
            }
            return res;
            }        
            else {
               System.out.println("ce n'est pas un chiffre hexadecimal");
                return -1;
            }
     
        }
     
        public String HexadecimalToBinaire(String arg){
            if (Pattern.matches("([0-9]|[a-f]|[A-F])*",arg)) {
                return this.DecimalToBinaire(HexadecimalToDecimal(arg));
            }        
            else {
                return "ce n'est pas un nombre hexadecimal";
            }        
        }
     
        public String HexadecimalToOctal(String arg){
            if (Pattern.matches("([0-9]|[a-f]|[A-F])*",arg)) {
                return this.DecimalToOctal(HexadecimalToDecimal(arg));
            }        
            else {
                return "ce n'est pas un nombre hexadecimal";
            }       
        }
     
        public String BinaireToOctal(String arg){
            if (Pattern.matches("[0-1]*",arg)) {
                return this.DecimalToOctal(BinaireToDecimal(arg));
            }        
            else {
                return "ce n'est pas un nombre binaire";
            }       
        }
     
        public String BinaireToHexadecimal(String arg){
            if (Pattern.matches("[0-1]*",arg)) {
                return this.DecimalToHexadecimal(BinaireToDecimal(arg));
            }        
            else {
                return "ce n'est pas un nombre binaire";
            }         
        }
     
        public String OctalToHexadecimal(String arg){
             if (Pattern.matches("[0-7]*",arg)) {
                return this.DecimalToHexadecimal(OctalToDecimal(arg));
            }        
            else {
                return "ce n'est pas un nombre octal";
            } 
        }
     
        public String OctalToBinaire(String arg){
            if (Pattern.matches("[0-7]*",arg)) {
                return this.DecimalToBinaire(OctalToDecimal(arg));
            }        
            else {
                return "ce n'est pas un nombre octal";
            }        
        }
     
        public static void main(String args[]){
            convert con=new convert();
     
            System.out.println("----------------- sans erreur ------------------");
     
            System.out.println(con.DecimalToHexadecimal(46));
            System.out.println(con.DecimalToOctal(12));
            System.out.println(con.DecimalToBinaire(46));
     
            System.out.println(con.BinaireToDecimal("100101100"));
            System.out.println(con.BinaireToOctal("10011100"));
            System.out.println(con.BinaireToHexadecimal("10011100"));
     
            System.out.println(con.OctalToDecimal("123"));
            System.out.println(con.OctalToBinaire("55"));
            System.out.println(con.OctalToHexadecimal("77"));
     
            System.out.println(con.HexadecimalToDecimal("a4"));        
            System.out.println(con.HexadecimalToBinaire("e8"));
            System.out.println(con.HexadecimalToOctal("ff"));
     
            System.out.println("\n\n\n----------------- avec erreur ------------------");
     
     
            System.out.println(con.BinaireToDecimal("100171100"));
            System.out.println(con.BinaireToOctal("10011g100"));
            System.out.println(con.BinaireToHexadecimal("10065411100"));
     
            System.out.println(con.OctalToDecimal("1283"));
            System.out.println(con.OctalToBinaire("595"));
            System.out.println(con.OctalToHexadecimal("7v7"));
     
            System.out.println(con.HexadecimalToDecimal("aze"));
            System.out.println(con.HexadecimalToBinaire("kf"));
            System.out.println(con.HexadecimalToOctal("azeaze"));
     
     
             System.out.println("----------------- FIN !!!!!!!!!! ------------------");
        }
    }

  3. #3
    Modérateur
    Avatar de joel.drigo
    Homme Profil pro
    Ingénieur R&D - Développeur Java
    Inscrit en
    Septembre 2009
    Messages
    12 430
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 54
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Ingénieur R&D - Développeur Java
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Septembre 2009
    Messages : 12 430
    Points : 29 131
    Points
    29 131
    Billets dans le blog
    2
    Par défaut
    Salut,

    Les classes Integer et Long, avec leur méthode toString(value, base) et parseInt(stringValue) et parseLong(stringValue) (ou valueOf(...)) font déjà ça nativement.
    L'expression "ça marche pas" ne veut rien dire. Indiquez l'erreur, et/ou les comportements attendus et obtenus, et donnez un Exemple Complet Minimal qui permet de reproduire le problème.
    La plupart des réponses à vos questions sont déjà dans les FAQs ou les Tutoriels, ou peut-être dans une autre discussion : utilisez la recherche interne.
    Des questions sur Java : consultez le Forum Java. Des questions sur l'EDI Eclipse ou la plateforme Eclipse RCP : consultez le Forum Eclipse.
    Une question correctement posée et rédigée et vous aurez plus de chances de réponses adaptées et rapides.
    N'oubliez pas de mettre vos extraits de code entre balises CODE (Voir Mode d'emploi de l'éditeur de messages).
    Nouveau sur le forum ? Consultez Les Règles du Club.

Discussions similaires

  1. [Toutes versions] Questions pour convertir des nombres stockés en format texte
    Par antoisse3 dans le forum Excel
    Réponses: 7
    Dernier message: 20/04/2015, 09h55
  2. Réponses: 1
    Dernier message: 05/12/2011, 10h25
  3. Convertir des float en decimal
    Par olibara dans le forum MS SQL Server
    Réponses: 2
    Dernier message: 29/08/2011, 16h48
  4. Convertir des nombres en texte
    Par BEKH_ dans le forum Excel
    Réponses: 4
    Dernier message: 11/06/2008, 14h43
  5. [ABAP] Convertir nombre decimal en binaire
    Par danael dans le forum SAP
    Réponses: 3
    Dernier message: 04/04/2005, 12h17

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