I have a helper function that will create a key or vector byte array for use in encryption methods. However I need a method that will take the byte[]
and output the following representation of the values as a string
from the byte array:
//I need the output to look like this:
"{241, 253, 159, 1, 153, 77, 115, 174, 234, 157, 77, 23, 34, 14, 19, 182, 65, 94, 71, 166, 86, 84, 50, 15, 133, 175, 8, 162, 248, 251, 38, 161}"
I found this long hand method to use which technically works but it's a mess, especially with having to remove the last comma:
public static string ByteArrayToString(byte[] byteArray)
{
var hex = new StringBuilder(byteArray.Length * 2);
foreach (var b in byteArray)
hex.AppendFormat("{0}, ", b);
var output = "{"+ hex.ToString() + "}";
return output.Remove(output.Length - 3, 2); //yuck
}
This seems to be a highly asked question and I found several posts with solutions, but none of the suggestions output the string from the byte[]
as I need above. I checked the following:
byte[] to hex string
How do you convert Byte Array to Hexadecimal String, and vice versa?
I used several of the parsing and LINQ examples but none of them output the byte array elements in the string as I needed above.
Is there a way to convert the actual values of the byte array returned from my helpwer method to the string format I need without using that hack of a method?