Convert from string ascii to string Hex
Solution 1
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
Solution 2
This seems the job for an 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();
}
}
A few tips.
Do not use string concatenation. Strings are immutable and thus every time you concatenate a string a new one is created. (Pressure on memory usage and fragmentation) A StringBuilder is generally more efficient for this case.
Strings are array of characters and using a foreach on a string already gives access to the character array
These common codes are well suited for an extension method included in a utility library always available for your projects (code reuse)
Solution 3
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);
Solution 4
A nice declarative way to solve this would be:
var str = "1234"
string.Join(" ", str.Select(c => $"0x{(int)c:X}"))
// Outputs "0x31 0x32 0x33 0x34"
Solution 5
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);
}
Output will be;
0X31 0X32 0X33 0X34
Here is a DEMO
.
cheziHoyzer
Updated on March 10, 2020Comments
-
cheziHoyzer about 4 years
Suppose I have this string
string str = "1234"
I need a function that convert this string to this string:
"0x31 0x32 0x33 0x34"
I searched online and found a lot of similar things, but not an answer to this question.
-
Daniel Hilgarth about 11 yearsThat outputs 0x1, 0x2, 0x3, 0x4. Not what the OP wants.
-
Daniel Hilgarth about 11 yearsYou should delete the first, incorrect version. And you should fix the second one. It produces the correct output, but the format should look like this:
string.Format("0x{0:X2}", (int)s)
. Reasons: (1)X8
is never used, because the value used for this parameter is a string. (2) Achar
can contain values that exceed the range of a byte. -
Davut Gürbüz about 11 yearsI just desired to show string.Format and ToString() together. I supposed chars in ASCII. byte is enough for ascii AFAIK. thanks.
-
Laurynas Lazauskas about 4 yearsNote that this uses
string
concatenation, which might or might not be a performance problem depending on the size ofstr
.