tags:

views:

234

answers:

3

hello, I am reading a file by binaryReader to a byte array but I want this array to be a 7 bit not 8 what can I use (utf7encoding)? Thanks.

+1  A: 

If you want to read a file encoded with utf7 charset, don't use BinaryReader.

Try an approach like this (assuming your input is a line separated text file):

StreamReader reader = new StreamReader(@"InputFile.txt", System.Text.Encoding.UTF7);
string sLine;
while((sLine = reader.ReadLine()) != null)
{
// Process the line
}
Tarnschaf
+1  A: 

hi, just read the entire file as usual (with the binaryreader), and then AND all the values with 127 (thus stripping the highest bit)

like so:

   value &= 127;  // Strip highest bit (effectively making it a 7 bit value)
Toad
This would be fine it that kind of data loss is acceptable in the application. IOW, those top bits if set needn't be and can be discarded.
AnthonyWJones
A: 

I'm going to guess you are trying to shove a binary file through some transport which limits usable bits in a byte to just the first 7.

If this out on a limb guess is correct then base64 encoding may fit the bill. For example assuming the file isn't huge:-

var content = File.ReadAllBytes("c:\yourpath");
var base64Content = Convert.ToBase64String(content);
var base64Array = System.Text.Encoding.ASCII.GetBytes(base64Content);

If the file is large then this approach can fairly easily be converted to a stream based approach so that chunks of the file can be encoded.

Of course for this to work the other end of the transport needs to be able to decode Base64 as well.

AnthonyWJones