Given a C# string which is a set of hexadecimal numbers such as:
string HexString = "202048656c6c6f20576f726c64313233212020";
Where those hexadecimal numbers represent the ASCII text:
" Hello World123! "
I need to convert the HexString to a String and I have the following code which works, but looks so excessive!
string HexStringToString(string HexString) {
string stringValue = "";
for (int i = 0; i < HexString.Length / 2; i++) {
string hexChar = HexString.Substring(i * 2, 2);
int hexValue = Convert.ToInt32(hexChar, 16);
stringValue += Char.ConvertFromUtf32(hexValue);
}
return stringValue;
}
Am I missing some elegant method?
static
as it appears that it doesn't access any class instance data. Further, it looks like an excellent candidate to be an extension method (simply change the parameter to readthis string HexString
after making the methodstatic
). – Jesse C. Slicer 6 hours ago