// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; using System.Globalization; using System.Text; namespace E { public class MainClass { public static string ToHex(byte[] ba) { StringBuilder sb = new StringBuilder(2 * ba.Length); for(int i = 0; i < ba.Length; i++) { sb.Append(ba[i].ToString("X2")); } return sb.ToString(); } public static byte[] FromHex(string s) { byte[] ba = new byte[s.Length/2]; for(int i = 0; i < ba.Length; i++) { ba[i] = byte.Parse(s.Substring(2 * i, 2), NumberStyles.HexNumber); } return ba; } public static void Main(string[] args) { byte[] ba = { 1, 2, 3, 4, 255 }; string s = ToHex(ba); Console.WriteLine(s); byte[] ba2 = FromHex(s); foreach(byte b in ba2) Console.Write(" " + b); Console.WriteLine(); } } }