begin process at 2012 02 11 18:46:02
  Trouver un code source :
 
dans
 
Accueil > 

Code

 > 

.NET

 > [C#] CONVERSION DE CHAÎNE 'STRING' VERS CHAÎNE 'DÉCIMAL' ET RÉCIPROQUEMENT

[C#] CONVERSION DE CHAÎNE 'STRING' VERS CHAÎNE 'DÉCIMAL' ET RÉCIPROQUEMENT


 Information sur la source

Note :
7,67 / 10 - par 3 personnes
7,67 / 10

  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 7

  • 8

  • 9

  • 10
Catégorie :.NET Source .NET ( DotNet ) Classé sous :tranformation, conversion, chaine, string, decimal Niveau :Débutant Date de création :15/07/2004 Vu :28 276

Auteur : scoubidou944

Ecrire un message privé
Commentaire sur cette source (1)
Ajouter un commentaire et/ou une note

 Description

Un bon exemple valant mieux qu'une tonne de commentaires :

string szStr, szStr2, szStr3;

szStr = "abcdefghijklmnopqrstuvwxyz";
szStr2 = ConvertAsciiStringToHexaString (szStr);
szStr3 = ConvertHexaStringToAsciiString (szStr2);

szStr = "Unicode character Pi(\u03a0)";
szStr2 = ConvertUnicodeStringToHexaString (szStr);
szStr3 = ConvertHexaStringToUnicodeString (szStr2);

Source

  • /// <summary>
  • /// Convert a string like 'abcdefghijklmnopqrstuvwxyz' to decimal string like '6162636465666768696A6B6C6D6E6F707172737475767778797A'
  • /// ONLY UNICODE IN THIS FUNCTION
  • /// </summary>
  • /// <param name="_szStr">String to convert</param>
  • /// <returns></returns>
  • public string ConvertUnicodeStringToHexaString (string _szStr)
  • {
  • string szResult = String.Empty;
  • byte[] abytesText = Encoding.Unicode.GetBytes(_szStr);
  • foreach (byte a in abytesText)
  • {
  • if (a<16)
  • szResult += "0" + a.ToString ("X");
  • else
  • szResult += a.ToString ("X");
  • }
  • return szResult;
  • }
  • /// <summary>
  • /// Convert a string like '6162636465666768696A6B6C6D6E6F707172737475767778797A' to decimal string like 'abcdefghijklmnopqrstuvwxyz'
  • /// ONLY UNICODE IN THIS FUNCTION
  • /// </summary>
  • /// <param name="_szStr">String to convert</param>
  • /// <returns></returns>
  • public string ConvertHexaStringToUnicodeString (string _szStr)
  • {
  • int iOffset = 0;
  • string szResult = String.Empty;
  • byte[] abytesText = new byte [_szStr.Length /2];
  • for (int i = 0; i < _szStr.Length /2; i++)
  • {
  • abytesText [iOffset] = (byte)Int32.Parse(_szStr.Substring(i*2,2), System.Globalization.NumberStyles.HexNumber);
  • iOffset++;
  • }
  • char[] asciiChars = new char[Encoding.Unicode.GetCharCount(abytesText, 0, abytesText.Length)];
  • Encoding.Unicode.GetChars(abytesText, 0, abytesText.Length, asciiChars, 0);
  • szResult = new string(asciiChars);
  • return szResult;
  • }
  • /// <summary>
  • /// Convert a string like 'abcdefghijklmnopqrstuvwxyz' to decimal string like '6162636465666768696A6B6C6D6E6F707172737475767778797A'
  • /// ONLY ASCII IN THIS FUNCTION
  • /// </summary>
  • /// <param name="_szStr">String to convert</param>
  • /// <returns></returns>
  • public string ConvertAsciiStringToHexaString (string _szStr)
  • {
  • string szResult = String.Empty;
  • byte[] abytesText = Encoding.ASCII.GetBytes(_szStr);
  • foreach (byte a in abytesText)
  • {
  • if (a<16)
  • szResult += "0" + a.ToString ("X");
  • else
  • szResult += a.ToString ("X");
  • }
  • return szResult;
  • }
  • /// <summary>
  • /// Convert a string like '6162636465666768696A6B6C6D6E6F707172737475767778797A' to decimal string like 'abcdefghijklmnopqrstuvwxyz'
  • /// ONLY ASCII IN THIS FUNCTION
  • /// </summary>
  • /// <param name="_szStr">String to convert</param>
  • /// <returns></returns>
  • public string ConvertHexaStringToAsciiString (string _szStr)
  • {
  • int iOffset = 0;
  • string szResult = String.Empty;
  • byte[] abytesText = new byte [_szStr.Length /2];
  • for (int i = 0; i < _szStr.Length /2; i++)
  • {
  • abytesText [iOffset] = (byte)Int32.Parse(_szStr.Substring(i*2,2), System.Globalization.NumberStyles.HexNumber);
  • iOffset++;
  • }
  • char[] asciiChars = new char[Encoding.ASCII.GetCharCount(abytesText, 0, abytesText.Length)];
  • Encoding.ASCII.GetChars(abytesText, 0, abytesText.Length, asciiChars, 0);
  • szResult = new string(asciiChars);
  • return szResult;
  • }
/// <summary>
		/// Convert a string like 'abcdefghijklmnopqrstuvwxyz' to decimal string like '6162636465666768696A6B6C6D6E6F707172737475767778797A'
		/// ONLY UNICODE IN THIS FUNCTION
		/// </summary>
		/// <param name="_szStr">String to convert</param>
		/// <returns></returns>
		public string ConvertUnicodeStringToHexaString (string _szStr)
		{
			string szResult		= String.Empty;
			byte[] abytesText	= Encoding.Unicode.GetBytes(_szStr);

			foreach (byte a in abytesText)
			{
				if (a<16)
					szResult += "0" + a.ToString ("X");
				else
					szResult += a.ToString ("X");
			}
			return szResult;
		}

		/// <summary>
		/// Convert a string like '6162636465666768696A6B6C6D6E6F707172737475767778797A' to decimal string like 'abcdefghijklmnopqrstuvwxyz'
		/// ONLY UNICODE IN THIS FUNCTION
		/// </summary>
		/// <param name="_szStr">String to convert</param>
		/// <returns></returns>
		public string ConvertHexaStringToUnicodeString (string _szStr)
		{
			int		iOffset		= 0;
			string	szResult	= String.Empty;
			byte[]	abytesText	= new byte [_szStr.Length /2];

			for (int i = 0; i < _szStr.Length /2; i++)
			{
				abytesText [iOffset] = (byte)Int32.Parse(_szStr.Substring(i*2,2), System.Globalization.NumberStyles.HexNumber);
				iOffset++;
			}

			char[] asciiChars = new char[Encoding.Unicode.GetCharCount(abytesText, 0, abytesText.Length)];
			Encoding.Unicode.GetChars(abytesText, 0, abytesText.Length, asciiChars, 0);
			szResult = new string(asciiChars);

			return szResult;
		}

		/// <summary>
		/// Convert a string like 'abcdefghijklmnopqrstuvwxyz' to decimal string like '6162636465666768696A6B6C6D6E6F707172737475767778797A'
		/// ONLY ASCII IN THIS FUNCTION
		/// </summary>
		/// <param name="_szStr">String to convert</param>
		/// <returns></returns>
		public string ConvertAsciiStringToHexaString (string _szStr)
		{
			string szResult		= String.Empty;
			byte[] abytesText	= Encoding.ASCII.GetBytes(_szStr);

			foreach (byte a in abytesText)
			{
				if (a<16)
					szResult += "0" + a.ToString ("X");
				else
					szResult += a.ToString ("X");
			}
			return szResult;
		}

		/// <summary>
		/// Convert a string like '6162636465666768696A6B6C6D6E6F707172737475767778797A' to decimal string like 'abcdefghijklmnopqrstuvwxyz'
		/// ONLY ASCII IN THIS FUNCTION
		/// </summary>
		/// <param name="_szStr">String to convert</param>
		/// <returns></returns>
		public string ConvertHexaStringToAsciiString (string _szStr)
		{
			int		iOffset		= 0;
			string	szResult	= String.Empty;
			byte[]	abytesText	= new byte [_szStr.Length /2];

			for (int i = 0; i < _szStr.Length /2; i++)
			{
				abytesText [iOffset] = (byte)Int32.Parse(_szStr.Substring(i*2,2), System.Globalization.NumberStyles.HexNumber);
				iOffset++;
			}

			char[] asciiChars = new char[Encoding.ASCII.GetCharCount(abytesText, 0, abytesText.Length)];
			Encoding.ASCII.GetChars(abytesText, 0, abytesText.Length, asciiChars, 0);
				
			szResult = new string(asciiChars);
			return szResult;
		}

 Conclusion

Pour tous ceux qui utilisent du cryptage (AES par exemple qui a le don de produire des chaînes avec des caractères ASCII à la con), voilà comment comment convertir une chaîne vers sa valeur décimale lisible dans n'importe quel fichier TXT.


 Sources du même auteur

Source avec Zip Source avec une capture Source .NET (Dotnet) [C#] [XML] SERIALIZATION STRING, COLOR, ARRAYLIST, HASHTABLE...
Source avec Zip Source avec une capture Source .NET (Dotnet) [C#] COLLAPSIBLE PANEL
Source avec Zip Source avec une capture Source .NET (Dotnet) TREELISTVIEW
Source avec Zip Source avec une capture Source .NET (Dotnet) [C#] [TUTORIAL BASIQUE] STÉGANOGRAPHIE IMAGE BITMAP
Source .NET (Dotnet) [C#] TUTORIAL BASIQUE (FICHIER BINAIRE, SÉRIALISATION)

 Sources de la même categorie

Source avec Zip Source avec une capture Source .NET (Dotnet) ORIONBANQUE par toutphp
Source avec Zip Source avec une capture Source .NET (Dotnet) ORIONAPPLICATION par toutphp
Source avec Zip SOCKET CONNEXION CLIENT & SERVEUR par ziedto83
Source avec Zip Source .NET (Dotnet) FFMPEG.NET : WRAPPER .NET DE FFMPEG par MasterShadows
Source avec Zip Source .NET (Dotnet) ATTACHER, CRÉER ET SAUVEGARDER UNE BASE DE DONNÉES SQL SERVE... par Alvepinai

 Sources en rapport avec celle ci

Source avec Zip Source .NET (Dotnet) FFMPEG.NET : WRAPPER .NET DE FFMPEG par MasterShadows
Source avec Zip Source .NET (Dotnet) CLASSE XMLMAPPER par Warny
Source avec Zip Source avec une capture Source .NET (Dotnet) GESTION DES LANGUES, COUNTRIES, CHAÎNES DE CARACTÈRE SIMPLE par gourky
Source avec Zip Source avec une capture Source .NET (Dotnet) SIMPLECONV - APPLICATION DE CONVERSION MONÉTAIRE AVEC TAUX E... par Jeffrey_
Source avec Zip Source .NET (Dotnet) UNE DLL QUI CONTIENT TOUTES LES METHODES DE CONVERSION D'UN ... par lino03

Commentaires et avis

Commentaire de lucienassaillit le 05/11/2010 09:26:08

Bonjour scoubidou944,

Merci pour ces beaux exemples. J'utilise ce site justement pour avoir pour un accés rapide à ce genre de bouts de code ou de classes , bien faits et trés utiles. Je promets de renvoyer la pareille avec un testeur de classes enfouies dans plusieurs dlls.
Cdlt,
Lucien

 Ajouter un commentaire


Discussions en rapport avec ce code source dans le forum

Conversion d'un string en XML [ par swyms ] Bonjour à tous,Comment faire pour convertir une chaine en un objet XML ?En fait, j'ai une chaine de caractère correctement formatté avec les balises q Conversion d'une chaine String en Class [ par PROTEUS91 ] Bonjour, Je poste ce message car je ne trouve pas de solution a mon problème. J'ai une chaine string : string _sClassName="MyClass" La classe MyCl Formatage string en une date [ par bouyahya ] Bonjour, comment formater un chaine de la forme 20150930 en une chaine de la forme 30/09/2015?? Merciiiii WP7 Conversion string / double [ par jihednond ] Je suis actuellement entrain de développer une application pour Windows Phone 7 (C#)qui consiste à lire à partir d'un fichier xml la latitude et la lo Changer couleur d'un string [ par kdesigner ] Bonjour j'aurais voulu savoir s'il était possible de changer la couleur basique noire d'une chaine en une autre couleur je m'explique: string str=" Accents en c# [ par grogoin ] Bonjour, je voudrais réussir a transformé des String du type "grégou" en "gr& eacute;gou". J'ai fais provisoirement un code qui changent manuellemen Problèeme de conversion int en string [ par florianj54 ] Bonjour, j'ai une liste une liste d'objet animaux et je récupère leur type, si c'est un felin, un rapace etc .. seulement quand je veux afficher le Conversion string - octet [ par trdev ] Bonjourje dois récupérer les infos d'un champs Acces (texte) d'une application.D'apres l'éditeur les champs est un  octet binaire encadré de guillemet covertir string en float [ par baby85 ] bonsoir,je suis débutante en c# et je veux savoir comment peux-je convertir une chaine de caractére string en float avec cette chaine  est le résultat Ecriture en mémoire d'une chaine de caractère [ par ScRunCHy ] Salut à tous,Dans mon code en C#, je récupère une string qui est écrite dans un fichier XML de config. Mon but est d'obtenir un char*.J'ai d'abord uti


Nos sponsors


Sondage...

Comparez les prix

CalendriCode

Février 2012
LMMJVSD
  12345
6789101112
13141516171819
20212223242526
272829    

Consulter la suite du CalendriCode

 
Développement réalisé par Nicolas SOREL (Nix) avec l'aide de : Cyril DURAND et Emmanuel (EBArtSoft), Merci à Vincent pour ses précieux conseils.
CodeS-SourceS.com© Toute reproduction même partielle est interdite sauf accord écrit du Webmaster
CodeS-SourceS.com© est une marque déposée tous droits réservés

Google Coop CodeS-SourceS Google Coop CodeS-SourceS
Temps d'éxécution de la page : 0,530 sec (4)

Nous contacter | Annoncer sur CodeS-SourceS | Mentions légales