I have the following test that I’ve been trying to get working
[Test]
public void Test()
{
byte[] testArray = new byte[] { 1, 0, 0, 1 };
string number = System.Text.Encoding.ASCII.GetString(testArray);
Assert.That(number, Is.EqualTo("1001"));
}
I get the following error
String lengths are both 4. Strings differ at index 0.
Expected: "1001"
But was: "\0\0"
This puzzles me because my production code that converts a byte array to a string uses the same method then I log out the string and it’s what I would expect.
Should I be doing something to the byte array before doing the conversion in this case?
Thanks, Neil
Byte 0 in ASCII is not “0” but some escape character.
This will probably fix your issue;