This commit is contained in:
44
util/HexString.cs
Normal file
44
util/HexString.cs
Normal file
@@ -0,0 +1,44 @@
|
||||
using System;
|
||||
using System.Text;
|
||||
|
||||
|
||||
namespace rockfishCore.Util
|
||||
{
|
||||
|
||||
public static class HexString
|
||||
{
|
||||
|
||||
|
||||
public static string ToHex(string str)
|
||||
{
|
||||
var sb = new StringBuilder();
|
||||
|
||||
var bytes = Encoding.ASCII.GetBytes(str);
|
||||
foreach (var t in bytes)
|
||||
{
|
||||
sb.Append(t.ToString("X2"));
|
||||
}
|
||||
|
||||
return sb.ToString(); // returns: "48656C6C6F20776F726C64" for "Hello world"
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static string FromHex(string hexString)
|
||||
{
|
||||
var bytes = new byte[hexString.Length / 2];
|
||||
for (var i = 0; i < bytes.Length; i++)
|
||||
{
|
||||
bytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
|
||||
}
|
||||
|
||||
return Encoding.ASCII.GetString(bytes); // returns: "Hello world" for "48656C6C6F20776F726C64"
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
//eoc
|
||||
}
|
||||
//eons
|
||||
}
|
||||
Reference in New Issue
Block a user