public static string AsciiToHex(string asciiString) { StringBuilder builder = new StringBuilder(); foreach (char c in asciiString) { builder.Append(Convert.ToInt32(c).ToString("X")); } return builder.ToString(); }
using System; using System.Collections; using System.Collections.Generic; using System.Data; using System.Diagnostics; class Convert { public void DoConversion() { string str = "test"; byte[] byteArray = null; System.Text.StringBuilder hexNumbers = new System.Text.StringBuilder(); byteArray = System.Text.ASCIIEncoding.ASCII.GetBytes(str); for (int i = 0; i <= byteArray.Length - 1; i++) { hexNumbers.Append(byteArray[i].ToString("x")); } Interaction.MsgBox(hexNumbers.ToString()); // Out put will be 74657374 // The following code will reverse the operation and verify the output: string st = hexNumbers.ToString(); string com = ""; for (x = 0; x <= st.Length - 1; x += 2) { com += Strings.ChrW(Convert.ToInt32("&H" + st.Substring(x, 2))); } Interaction.MsgBox(com); } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)