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 :

[C#]Comment convertir un System.object en string ?


Sujet :

C#

  1. #1
    Membre confirm� Avatar de hamster
    Profil pro
    Inscrit en
    Octobre 2003
    Messages
    137
    D�tails du profil
    Informations personnelles :
    �ge : 41
    Localisation : France, Rh�ne (Rh�ne Alpes)

    Informations forums :
    Inscription : Octobre 2003
    Messages : 137
    Par d�faut [C#]Comment convertir un System.object en string ?
    Bonjour,

    je cherche � convertir un System.object contenant un tableau de Byte en une chaine de caract�re.
    Par exemple, mon array contient
    Code : S�lectionner tout - Visualiser dans une fen�tre � part
    1
    2
    3
    4
    5
    6
    7
    8
    [0]
    [0]
    [0]
    [0]
    [0]
    [0]
    [0]
    [188]
    Et j'aimerais r�cup�rer un string contenantJ'ai fouill� dans la FAQ, effectu� une recherche sur le forum, consult� MSDN, essay� des tas de m�thodes de System.Convert, mais je n'ai rien trouv�

    Pouvez vous m'aider ?

    Merci

  2. #2
    R�dacteur
    Avatar de abelman
    Inscrit en
    F�vrier 2003
    Messages
    1 106
    D�tails du profil
    Informations forums :
    Inscription : F�vrier 2003
    Messages : 1 106
    Par d�faut
    Bonjour,

    Ton System Object sera t il toujours un tableau d'entier?

  3. #3
    Membre exp�riment� Avatar de toniolol
    Homme Profil pro
    Chef de projet en SSII
    Inscrit en
    Juin 2005
    Messages
    281
    D�tails du profil
    Informations personnelles :
    Sexe : Homme
    �ge : 48
    Localisation : France, Gard (Languedoc Roussillon)

    Informations professionnelles :
    Activit� : Chef de projet en SSII
    Secteur : High Tech - �diteur de logiciels

    Informations forums :
    Inscription : Juin 2005
    Messages : 281
    Par d�faut
    Salut,
    ne peut tu pas parcourir ton tableau et utiliser la fonction Chr ?

    Par contre je n'ai pas compris o� passent tes 0 ?

    Code : S�lectionner tout - Visualiser dans une fen�tre � part
    1
    2
    3
    4
    dim tonResult as String
    For i as interger = 0 to tonTab.Lengh - 1
       tonResult &= Chr(tonTab(i))
    Next i
    Je n'ai pas test�...
    Par contre si �a fonctionne je crois qu'il vaut mieux utiliser un StringBuilder qu'un string avec &=

  4. #4
    Membre confirm� Avatar de hamster
    Profil pro
    Inscrit en
    Octobre 2003
    Messages
    137
    D�tails du profil
    Informations personnelles :
    �ge : 41
    Localisation : France, Rh�ne (Rh�ne Alpes)

    Informations forums :
    Inscription : Octobre 2003
    Messages : 137
    Par d�faut
    Citation Envoy� par abelman
    Bonjour,

    Ton System Object sera t il toujours un tableau d'entier?
    R�ponse : oui
    Citation Envoy� par toniolol
    Par contre je n'ai pas compris o� passent tes 0 ?
    Pouf pouf ils disparaissent. Mais c'est un d�tail. Si au final j'arrive � obtenir une chaine 0000000188 �a me suffit.
    Par contre, l'exemple que tu m'as donn� est en VB, pas en C#
    Est-ce que la m�thode Chr existe aussi en C# ?

  5. #5
    Membre exp�riment� Avatar de toniolol
    Homme Profil pro
    Chef de projet en SSII
    Inscrit en
    Juin 2005
    Messages
    281
    D�tails du profil
    Informations personnelles :
    Sexe : Homme
    �ge : 48
    Localisation : France, Gard (Languedoc Roussillon)

    Informations professionnelles :
    Activit� : Chef de projet en SSII
    Secteur : High Tech - �diteur de logiciels

    Informations forums :
    Inscription : Juin 2005
    Messages : 281
    Par d�faut
    Citation Envoy� par hamster
    Est-ce que la m�thode Chr existe aussi en C# ?
    Citation Envoy� par MSDN
    Espace de noms : Microsoft.VisualBasic

  6. #6
    Membre confirm� Avatar de hamster
    Profil pro
    Inscrit en
    Octobre 2003
    Messages
    137
    D�tails du profil
    Informations personnelles :
    �ge : 41
    Localisation : France, Rh�ne (Rh�ne Alpes)

    Informations forums :
    Inscription : Octobre 2003
    Messages : 137
    Par d�faut


    J'ai �crit une petite m�thode
    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
    protected string IdToString(object vntId)
    	{
    	        try
    		{
    			string strResult = "";
    			short i = 0;
    			object[] objId = (object[]) vntId;
    			while (System.Convert.ToInt32(objId[i]) == 0)
    			{
    				i++;	
    			}
    			for (int j = i ; j<8 ; j++)
    			{
    				strResult += (System.Convert.ToString(objId[j]));
    			}
    
    			return strResult;
    		}
    		catch (Exception e)
    		{
    			throw new PivotalApplicationException(e.Message, e, m_rdaSystem);
    		}
    
    	}
    Mais je n'arrive pas � convertir mon object vntId en object[]

  7. #7
    R�dacteur
    Avatar de dev01
    Profil pro
    Inscrit en
    Mai 2004
    Messages
    2 451
    D�tails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2004
    Messages : 2 451
    Par d�faut
    Citation Envoy� par hamster

    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
    protected string IdToString(Array vntId)
        {
                try
            {
                string strResult = "";
                short i = 0;
                object[] objId = (object[]) vntId;
                while (System.Convert.ToInt32(objId[i]) == 0)
                {
                    i++;    
                }
                for (int j = i ; j<8 ; j++)
                {
                    strResult += (System.Convert.ToString(objId[j]));
                }
    
                return strResult;
            }
            catch (Exception e)
            {
                throw new PivotalApplicationException(e.Message, e, m_rdaSystem);
            }
    
        }
    il faut que tu passes un Array en argument � ta fonction.

    Tout les tableau ont comme classe de base la classe Array

  8. #8
    Membre confirm� Avatar de hamster
    Profil pro
    Inscrit en
    Octobre 2003
    Messages
    137
    D�tails du profil
    Informations personnelles :
    �ge : 41
    Localisation : France, Rh�ne (Rh�ne Alpes)

    Informations forums :
    Inscription : Octobre 2003
    Messages : 137
    Par d�faut
    Mon probl�me devient alors :

    Comment passer d'un type object � un type System.Array ?

  9. #9
    R�dacteur
    Avatar de dev01
    Profil pro
    Inscrit en
    Mai 2004
    Messages
    2 451
    D�tails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2004
    Messages : 2 451
    Par d�faut
    ben tu cast � l'appel.

    Code : S�lectionner tout - Visualiser dans une fen�tre � part
    1
    2
    3
    4
    5
     
    public void UneMethode()
    {
       IdToString((Array)obj);  
    }
    ce que le compilateur ne veux pas c'est un cast directement en tableau d'un type. La cast un objet en un autre objet

  10. #10
    Membre confirm� Avatar de hamster
    Profil pro
    Inscrit en
    Octobre 2003
    Messages
    137
    D�tails du profil
    Informations personnelles :
    �ge : 41
    Localisation : France, Rh�ne (Rh�ne Alpes)

    Informations forums :
    Inscription : Octobre 2003
    Messages : 137
    Par d�faut
    Merci beaucoup, �a marche tr�s bien !!

    (PS : finalement j'ai r�ussi � contourner le probl�me autrement, mais j'aurai au moins appris quelque chose)



    hop r�solu

+ R�pondre � la discussion
Cette discussion est r�solue.

Discussions similaires

  1. comment convertir un objet URL en String ?
    Par _LittleFlea_ dans le forum Langage
    R�ponses: 3
    Dernier message: 19/10/2009, 16h16
  2. Comment convertir du XML dans un String -> DataSet
    Par jimbolelephan dans le forum C#
    R�ponses: 2
    Dernier message: 12/06/2008, 15h28
  3. R�ponses: 10
    Dernier message: 04/05/2006, 23h55
  4. Comment convertir de l'hexadecimale au string ASCII ?
    Par Battosaiii dans le forum Algorithmes et structures de donn�es
    R�ponses: 1
    Dernier message: 17/03/2006, 19h04
  5. R�ponses: 12
    Dernier message: 31/01/2006, 21h46

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