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

C# Discussion :

Convertir de Binaire en Base 36 hexatridecimal (base 2 to 36)


Sujet :

C#

  1. #1
    Membre confirmé
    Profil pro
    Inscrit en
    Octobre 2007
    Messages
    1 002
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2007
    Messages : 1 002
    Points : 552
    Points
    552
    Par défaut Convertir de Binaire en Base 36 hexatridecimal (base 2 to 36)
    Bonjour !

    Je souhaite en .Net convertir de base 2 en base 36.
    J'ai essayé de chercher sur le net mais je ne trouve pas grand chose qui marche !

    par exemple je souhaite convertir:
    11111001111110111001101011001001111111111111111111111111111111111111111111111111
    Ce qui donne en base 36: 5C8HXNG277GFV30G d'après ce site http://www.kaagaard.dk/service/convert.htm

    Or avec le meilleurs code que j'ai pu trouver sur le net:
    J'obtiens 5C8HXNG277GFV30F !!! (A une lettre près ! Failed...)
    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
     
            public static String ConvertBase(int from, int to, String s)
            {
                //Return error if input is empty
                if (String.IsNullOrEmpty(s))
                {
                    return ("Error: Nothing in Input String");
                }
                //only allow uppercase input characters in string
                s = s.ToUpper();
     
                //only do base 2 to base 36 (digit represented by characters 0-Z)"
                if (from < 2 || from > 36 || to < 2 || to > 36)
                { return ("Base requested outside range"); }
     
                //convert string to an array of integer digits representing number in base:from
                int il = s.Length;
                int[] fs = new int[il];
                int k = 0;
                for (int i = s.Length - 1; i >= 0; i--)
                {
                    if (s[i] >= '0' && s[i] <= '9') { fs[k++] = (int)(s[i] - '0'); }
                    else
                    {
                        if (s[i] >= 'A' && s[i] <= 'Z') { fs[k++] = 10 + (int)(s[i] - 'A'); }
                        else
                        { return ("Error: Input string must only contain any of 0-9 or A-Z"); } //only allow 0-9 A-Z characters
                    }
                }
     
                //check the input for digits that exceed the allowable for base:from
                foreach (int i in fs)
                {
                    if (i >= from) { return ("Error: Not a valid number for this input base"); }
                }
     
                //find how many digits the output needs
                int ol = il * (from / to + 1);
                int[] ts = new int[ol + 10]; //assign accumulation array
                int[] cums = new int[ol + 10]; //assign the result array
                ts[0] = 1; //initialize array with number 1 
     
                //evaluate the output
                for (int i = 0; i < il; i++) //for each input digit
                {
                    for (int j = 0; j < ol; j++) //add the input digit 
                    // times (base:to from^i) to the output cumulator
                    {
                        cums[j] += ts[j] * fs[i];
                        int temp = cums[j];
                        int rem = 0;
                        int ip = j;
                        do // fix up any remainders in base:to
                        {
                            rem = temp / to;
                            cums[ip] = temp - rem * to; ip++;
                            cums[ip] += rem;
                            temp = cums[ip];
                        }
                        while (temp >= to);
                    }
     
                    //calculate the next power from^i) in base:to format
                    for (int j = 0; j < ol; j++)
                    {
                        ts[j] = ts[j] * from;
                    }
                    for (int j = 0; j < ol; j++) //check for any remainders
                    {
                        int temp = ts[j];
                        int rem = 0;
                        int ip = j;
                        do  //fix up any remainders
                        {
                            rem = temp / to;
                            ts[ip] = temp - rem * to; ip++;
                            ts[ip] += rem;
                            temp = ts[ip];
                        }
                        while (temp >= to);
                    }
                }
     
                //convert the output to string format (digits 0,to-1 converted to 0-Z characters) 
                String sout = String.Empty; //initialize output string
                bool first = false; //leading zero flag
                for (int i = ol; i >= 0; i--)
                {
                    if (cums[i] != 0) { first = true; }
                    if (!first) { continue; }
                    if (cums[i] < 10) { sout += (char)(cums[i] + '0'); }
                    else { sout += (char)(cums[i] + 'A' - 10); }
                }
                if (String.IsNullOrEmpty(sout)) { return "0"; } //input was zero, return 0
                //return the converted string
                return sout;
            }
    source : http://www.codeproject.com/Articles/...itive-Integers

    Si quelqu'un à déja traité ce sujet, je suis preneur d'info !

    Merci à tous !

  2. #2
    Expert éminent Avatar de Graffito
    Profil pro
    Inscrit en
    Janvier 2006
    Messages
    5 993
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2006
    Messages : 5 993
    Points : 7 903
    Points
    7 903
    Par défaut
    Il y a le code en JavaScript dans ton lien :
    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
        var digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    function xtoy(base) {
        var k = converter.input.value; // string
        var i = 0; // int
        var sum=0; // int
        var j = 0; // int
        // no comma
        while(k.charAt(i)!="." && i<=k.length) i++;
        k = k.substring(0,i);
        i=0;
         // change Lowercase to UpperCase
        text  = k
        var casechanged = text.toUpperCase(); // string
        k = casechanged
        // - minus sign, + minus sign
        var sign = 1;
        if (k.charAt(0) == "-") {
            sign=-1; // int
            k = k.substring(1,k.length);
            }
        var pot = k.length-1; // int
        // Convert
        while(pot>=0) {
            while (k.charAt(j)!=digits.charAt(i) && i<=base) i++;
                sum = sum + ((i) * Math.pow(base,pot));
                j++;
                pot--;
                i=0;    
            }
        ytox(sign*sum);
    }
    function ytox(z) {
        var base = converter.tobase.value; // string
        var xconverted; // string
        var k = z + ""; // string
        if (k.charAt(0) == "-") xconverted="-";
        else xconverted="";
        k = Math.abs(k); // int
        var j = 0;
        var i = 0;
        while(k>=Math.pow(base,(i+1))) i++;
            while(k>0){
                while (k>=((j+1)*Math.pow(base,i))) j++;
                k = k - (j)*Math.pow(base,i);
                xconverted = xconverted + digits.charAt(j);
                i--;
                j = 0;  
            }
        for(j=i;j>=0;j--) xconverted = xconverted + "0";
        converter.output.value = xconverted
    }
    " Le croquemitaine ! Aaaaaah ! Où ça ? " ©Homer Simpson

Discussions similaires

  1. [WD19] Comment convertir une analyse HFSQL Client/Serveur en base
    Par koukic11 dans le forum WinDev
    Réponses: 0
    Dernier message: 02/06/2015, 14h52
  2. Convertir un nombre décimal fractionnaire négatif en base 2
    Par christian79 dans le forum Algorithmes et structures de données
    Réponses: 8
    Dernier message: 09/04/2013, 13h55
  3. [MySQL] Convertir une base hyperfile en base mysql
    Par lordgodgiven dans le forum PHP & Base de données
    Réponses: 1
    Dernier message: 06/09/2007, 17h24
  4. Convertir un entier base 10 en base X ?
    Par titoumimi dans le forum Langage
    Réponses: 2
    Dernier message: 16/09/2006, 13h14
  5. Grant all sur toutes les bases sauf la base mysql
    Par titoff002 dans le forum SQL Procédural
    Réponses: 1
    Dernier message: 15/09/2005, 22h18

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