Skip to content

Instantly share code, notes, and snippets.

@fernandezja
Created February 14, 2018 11:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fernandezja/4a97ec3544060c54b09d16c2750e5d40 to your computer and use it in GitHub Desktop.
Save fernandezja/4a97ec3544060c54b09d16c2750e5d40 to your computer and use it in GitHub Desktop.
Converter de String to Hexadecimal / Convertidor de cadena a hexadecimal
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