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 :

Inversion chaine hexadécimale


Sujet :

C#

  1. #1
    Candidat au Club
    Profil pro
    Inscrit en
    Mai 2012
    Messages
    7
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2012
    Messages : 7
    Points : 4
    Points
    4
    Par défaut Inversion chaine hexadécimale
    Bonjour,

    J'ai cherché partout comment je pouvais faire mais pas moyen de trouver ce que je souhaite.
    Je voudrais inverser une chaîne hexa :
    Par exemple j'ai 01 23 15 et je voudrais l'inverser pour avoir 10 32 51

    J'ai essayé
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    public static string Reverse(string text)
    {
      char[] chars = text.ToCharArray();
      Array.Reverse(chars);
      return new String(chars);
    }
    ou
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
     
    public static string Reverse(string text)
    {
        return String.Concat(text.Reverse());
    }
    Mais à chaque fois, ça m'inverse toute la chaîne, alors que l'inversion doit se faire tous les 2 caractères. Cela n'est peut-être pas possible à faire.

    Merci.

  2. #2
    Membre éclairé
    Homme Profil pro
    Administrateur systèmes et réseaux
    Inscrit en
    Septembre 2011
    Messages
    610
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 35
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Administrateur systèmes et réseaux
    Secteur : High Tech - Opérateur de télécommunications

    Informations forums :
    Inscription : Septembre 2011
    Messages : 610
    Points : 713
    Points
    713
    Par défaut
    Hello,

    Utilise Split!!
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    public static string Reverse(string text)
    {
        string retour;
        string[] str = text.Split(' ');
        for(int i=0; i<=str.Length; i++){
             retour += String.Concat(str[1].Reverse());
             retour += " ";
        }
        return retour;
    }

  3. #3
    Candidat au Club
    Profil pro
    Inscrit en
    Mai 2012
    Messages
    7
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2012
    Messages : 7
    Points : 4
    Points
    4
    Par défaut
    En utilisant le split, voici ce que j'obtiens à l'écran :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    System.Linq.Enumerable+<ReverseIterator>d__99`1[System.Char] System.Linq.Enumerable+<ReverseIterator>d__99`1[System.Char]

  4. #4
    Expert confirmé
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Novembre 2009
    Messages
    2 032
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 35
    Localisation : France, Bouches du Rhône (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Développeur .NET

    Informations forums :
    Inscription : Novembre 2009
    Messages : 2 032
    Points : 5 476
    Points
    5 476
    Par défaut
    -_- je vois pas pourquoi il y aurait cette erreur avec le code fourni plus haut (mise à part qu'il manque la déclaration du tableau de string str[] )

    Tu peux fournir le code que tu fais?

  5. #5
    Candidat au Club
    Profil pro
    Inscrit en
    Mai 2012
    Messages
    7
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2012
    Messages : 7
    Points : 4
    Points
    4
    Par défaut
    Voici mon code,

    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
     
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
     
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
     
     
            public static string Reverse(string text)
            {
                string retour="";
                string[] str = text.Split(' ');
                for (int i = 0; i <= str.Length; i++)
                {
                    retour += String.Concat(str[1].Reverse());
                    retour += " ";
                }
                return retour;
            }
     
            private void button1_Click(object sender, EventArgs e)
            {
                textBox2.Text = Reverse(textBox1.Text);
            }
        }
    }

  6. #6
    Membre émérite Avatar de meziantou
    Homme Profil pro
    Ingénieur R&D
    Inscrit en
    Avril 2010
    Messages
    1 223
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 33
    Localisation : Canada

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

    Informations forums :
    Inscription : Avril 2010
    Messages : 1 223
    Points : 2 439
    Points
    2 439
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    retour += String.Concat(str[i].Reverse());

  7. #7
    Expert confirmé
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Novembre 2009
    Messages
    2 032
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 35
    Localisation : France, Bouches du Rhône (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Développeur .NET

    Informations forums :
    Inscription : Novembre 2009
    Messages : 2 032
    Points : 5 476
    Points
    5 476
    Par défaut
    Enleve le string concat, et remplace le 1 par i ce qui donne :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    retour += String.Concat(str[1].Reverse());
    Remplacé par
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    retour +=str[i].Reverse();
    Sinon je vois rien d'anormal

    Mais j'y pense, d'un point de vue algorithmique, ton entrée est bien du style "xx yy oo aa" avec un espace entre chaque binome? Sinon ca ne fera rien bien évidement...

  8. #8
    Candidat au Club
    Profil pro
    Inscrit en
    Mai 2012
    Messages
    7
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2012
    Messages : 7
    Points : 4
    Points
    4
    Par défaut
    Plus d'erreur comme celle de tout à l'heure mais maintenant j'ai
    L'index se trouve en dehors des limites du tableau.

  9. #9
    Expert confirmé
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Novembre 2009
    Messages
    2 032
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 35
    Localisation : France, Bouches du Rhône (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Développeur .NET

    Informations forums :
    Inscription : Novembre 2009
    Messages : 2 032
    Points : 5 476
    Points
    5 476
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    for (int i = 0; i < str.Length; i++)
    inferieur strict

  10. #10
    Candidat au Club
    Profil pro
    Inscrit en
    Mai 2012
    Messages
    7
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2012
    Messages : 7
    Points : 4
    Points
    4
    Par défaut
    Rah oui j'avais pas fais gaffe, par contre, ma merveilleuse erreur de tout à l'heure
    System.Linq.Enumerable+<ReverseIterator>d__99`1[System.Char] System.Linq.Enumerable+<ReverseIterator>d__99`1[System.Char] System.Linq.Enumerable+<ReverseIterator>d__99`1[System.Char]
    est de retour -_-'

  11. #11
    Expert confirmé
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Novembre 2009
    Messages
    2 032
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 35
    Localisation : France, Bouches du Rhône (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Développeur .NET

    Informations forums :
    Inscription : Novembre 2009
    Messages : 2 032
    Points : 5 476
    Points
    5 476
    Par défaut
    Tiens j'avais même pas remarque que reverse n'existe pas vraiment sur un string (méthode d'extension)...Et du coup je sais pas pourquoi ca bug mais tu peux faire ton propre reverse comme ceci:
    Rajoute
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    static public string ReverseString(string str)  
    {  
       char[] charArray = str.ToCharArray();  
       Array.Reverse( charArray );  
       return new string( charArray );  
    }
    et remplace
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    public static string Reverse(string text)
            {
                string retour="";
                string[] str = text.Split(' ');
                for (int i = 0; i <str.Length; i++)
                {
                    retour += ReverseString(str[1]);
                    retour += " ";
                }
                return retour;
            }

  12. #12
    Candidat au Club
    Profil pro
    Inscrit en
    Mai 2012
    Messages
    7
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2012
    Messages : 7
    Points : 4
    Points
    4
    Par défaut
    ça fonctionne

    Je vous remercie tous pour votre aide

    PS : j'en profite juste pour corriger ton code micka132, c'est bien
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    retour += ReverseString(str[i]);
    et non
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    retour += ReverseString(str[1]);
    qu'il faut mettre.

    Encore merci à tous

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

Discussions similaires

  1. [C#] Comment inverser une chaine de caractères ?
    Par just1980 dans le forum Contribuez
    Réponses: 6
    Dernier message: 01/05/2011, 20h35
  2. Réponses: 16
    Dernier message: 01/09/2008, 20h25
  3. Réponses: 1
    Dernier message: 10/04/2007, 23h29
  4. inverser chaine
    Par jejam dans le forum Oracle
    Réponses: 2
    Dernier message: 20/12/2005, 10h21
  5. chaine inverse (classique débutant)
    Par aA189 dans le forum C++
    Réponses: 3
    Dernier message: 24/11/2004, 12h01

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