Hadad
Hadad

Reputation: 344

converting from byte to 7 bit byte

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.

Upvotes: 0

Views: 2681

Answers (3)

AnthonyWJones
AnthonyWJones

Reputation: 189457

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.

Upvotes: 1

Toad
Toad

Reputation: 15925

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)

Upvotes: 2

Michael Cornel
Michael Cornel

Reputation: 4004

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
}

Upvotes: 1

Related Questions