I have a csv file with binary data inline with text (ie Text,0xFF00). I need to read in both values and save them into a database. Currently I’m using a StreamReader to read in the line and splitting it by the comma. The issue I’m having is I need to convert the binary data into it’s byte[] equivalent (ie 0xFF00 goes to {255,0}). Any kind of conversion with encoding will change the binary.
byte[] data= System.Text.Encoding.ASCII.GetBytes(SplitString[1]);
If the entire file was binary I could use File.ReadAllBytes(file) Is there a way to do this in memory without writing out the binary portion to a temporary file?
That’s a bad start. Any chance you could change it?
Basically, you shouldn’t try to read binary data as text in the first place. Doing so will almost certainly lose data. (You could use ISO-Latin-1, but it’s an icky workaround for a nasty problem.)
Instead, you’ll probably want to read all the data as binary, and convert only the relevant bits of it to text using
Encoding.GetString.How do you know which bits of your file are meant to be what in your file? (i.e. which bits are binary and which bits are text)
Just to check, your file really does contain the binary data, not a hex representation of the binary data, right? If it’s actually all text data, but some of it is binary data encoded into text using hex, that’ll make life much easier.