Convert from ascii string to Hex string

Suppose I have this line

string str = "1234" 

I need a function that converts this string to this string:

 "0x31 0x32 0x33 0x34" 

I searched the Internet and found many similar things, but did not answer this question.

+13
source share
7 answers
 string str = "1234"; char[] charValues = str.ToCharArray(); string hexOutput=""; foreach (char _eachChar in charValues ) { // Get the integral value of the character. int value = Convert.ToInt32(_eachChar); // Convert the decimal value to a hexadecimal value in string form. hexOutput += String.Format("{0:X}", value); // to make output as your eg // hexOutput +=" "+ String.Format("{0:X}", value); } //here is the HEX hexOutput //use hexOutput 
+18
source

This seems to be the job for the extension method.

 void Main() { string test = "ABCD1234"; string result = test.ToHex(); } public static class StringExtensions { public static string ToHex(this string input) { StringBuilder sb = new StringBuilder(); foreach(char c in input) sb.AppendFormat("0x{0:X2} ", (int)c); return sb.ToString().Trim(); } } 

Some tips.
Do not use string concatenation. Lines are immutable, and so every time you concatenate a line, a new one is created. (Pressure on memory usage and fragmentation). StringBuilder is usually more efficient for this case.

Strings are an array of characters, and using foreach in a string already gives access to an array of characters

These common codes are well suited for the extension method included in the useful library always available for your projects (code reuse)

+7
source
 static void Main(string[] args) { string str = "1234"; char[] array = str.ToCharArray(); string final = ""; foreach (var i in array) { string hex = String.Format("{0:X}", Convert.ToInt32(i)); final += hex.Insert(0, "0X") + " "; } final = final.TrimEnd(); Console.WriteLine(final); } 

The output will be:

 0X31 0X32 0X33 0X34 

Here is the DEMO .

+2
source

Convert to byte array and then to hex

  string data = "1234"; // Convert to byte array byte[] retval = System.Text.Encoding.ASCII.GetBytes(data); // Convert to hex and add "0x" data = "0x" + BitConverter.ToString(retval).Replace("-", " 0x"); System.Diagnostics.Debug.WriteLine(data); 
+1
source
  [TestMethod] public void ToHex() { string str = "1234A"; var result = str.Select(s => string.Format("0x{0:X2}", ((byte)s))); foreach (var item in result) { Debug.WriteLine(item); } } 
0
source

This is the one I used:

 private static string ConvertToHex(byte[] bytes) { var builder = new StringBuilder(); var hexCharacters = new[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; for (var i = 0; i < bytes.Length; i++) { int firstValue = (bytes[i] >> 4) & 0x0F; int secondValue = bytes[i] & 0x0F; char firstCharacter = hexCharacters[firstValue]; char secondCharacter = hexCharacters[secondValue]; builder.Append("0x"); builder.Append(firstCharacter); builder.Append(secondCharacter); builder.Append(' '); } return builder.ToString().Trim(' '); } 

And then used as:

 string test = "1234"; ConvertToHex(Encoding.UTF8.GetBytes(test)); 
0
source
  string data = richTextBox1.Text; // Convert to byte array byte[] retval = System.Text.Encoding.ASCII.GetBytes(data); // Convert to hex and add "0x" data = "0x" + BitConverter.ToString(retval).Replace("-", " 0x"); richTextBox1.Text = data; 

If you want to change the '0x' or completely remove the interval, just edit or delete this:

 data = "0x" + BitConverter.ToString(retval).Replace("-", " 0x"); 

This will work with almost anything (from buttons to menus).

-one
source

All Articles