0

I used the following code to convert a bytes array to a string:

byte[] saltBytes;

// ... filling the array with some values

for (int i = 0; i < saltBytes.Length; i++)
{
    builder.Append(saltBytes[i].ToString("x2"));
}

string salt = builder.ToString(); 

Now, I would like to revert this process and convert my string to a bytes array. I was reading this question: Converting string to byte array in C#.

The answer mentions:

If you already have a byte array then you will need to know what type of encoding was used to make it into that byte array.

I'm not sure about the encoding used in the snippet I posted. Does this have to do with the ToString("x2") part?

Community
  • 1
  • 1
  • You don't have to worry about encoding as you converting yourself: single byte to two characters (padded with `0` if less than `0xF`). – Sinatr Mar 28 '17 at 09:18
  • By "encoding" they are referring to UTF8, UTF16, etc. But as @Sinatr said, you don't need that. You just need to go from a hex string back to byte. – vyrp Mar 28 '17 at 09:21

0 Answers0