public void parse_table(BinaryReader inFile) { byte[] idstring = inFile.ReadBytes(6); Console.WriteLine(Convert.ToString(idstring)); }
It is a simple snippet: read the first 6 bytes of the file and convert that to a string.
However the console shows System.Byte[]
.
Maybe I'm using the wrong class for conversion. What should I be using? It will eventually be parsing filenames encoded in UTF-8, and I'm planning to use the same method to read all filenames.
This is just an updated version of Jesse Webbs code that doesn't append the unnecessary trailing
,
character.The output from this method would be:
For some fun with linq and string interpolation:
Test cases:
Output:
It's actually:
or for UTF-8 specifically:
I was in a predicament where I had a signed byte array (
sbyte[]
) as input to a Test class and I wanted to replace it with a normal byte array (byte[]
) for simplicity. I arrived here from a Google search but Tom's answer wasn't useful to me.I wrote a helper method to print out the initializer of a given
byte[]
:You can use it like this:
The ouput is valid C# which can then just be copied into your code.
And just for completeness, here is the
UnsignedBytesFromSignedBytes
method:I've used this simple code in my codebase:
To use: