Created
February 14, 2018 11:57
-
-
Save fernandezja/4a97ec3544060c54b09d16c2750e5d40 to your computer and use it in GitHub Desktop.
Converter de String to Hexadecimal / Convertidor de cadena a hexadecimal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Text; | |
namespace HexToStringConverter | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var hex = StringToHexConvertidor("StarWars!"); | |
Console.WriteLine("Hexadecimal > {0}", hex); | |
var cadena = HexToStringConvertidor(hex); | |
Console.WriteLine("String > {0}", cadena); | |
Console.ReadKey(); | |
} | |
private static string StringToHexConvertidor(string cadenaAConvertir) | |
{ | |
char[] valores = cadenaAConvertir.ToCharArray(); | |
var resultado = new StringBuilder(); | |
foreach (char caracter in valores) | |
{ | |
int valorInt32 = Convert.ToInt32(caracter); | |
string valorHex = String.Format("{0:X}", valorInt32); | |
resultado.Append(valorHex); | |
resultado.Append(" "); | |
} | |
return resultado.ToString(); | |
} | |
private static string HexToStringConvertidor(string cadenaHex) | |
{ | |
string[] valores = cadenaHex.Split(" "); | |
var resultado = new StringBuilder(); | |
foreach (String hex in valores) | |
{ | |
if (!string.IsNullOrEmpty(hex)) | |
{ | |
int valorInt16 = Convert.ToInt32(hex, 16); | |
string cadenaValor = Char.ConvertFromUtf32(valorInt16); | |
resultado.Append(cadenaValor); | |
} | |
} | |
return resultado.ToString(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment