Romain TAILLANDIER said:
what a pity !
I need to memorise some info on a very small transponder. i can know by
advance which size the file will have. So to optimise the space i will use a
6 bit codage table (like a 6 bit-ASCII-like) which is more than i need. so i
gain 25% space, which is not nothing.
but i want to know if there is fonction wich can read write a file by 6 bit
blocks. otherwise, i will have to write them in a C dll, with very low level
fonctions ..... which i prefere not.
i will wait a few more information before start that, if anybody have
information
It really wouldn't be hard to do in .NET though - there's no need to
resort to C. For instance, if you don't mind reading it one value at a
time, you could use something like:
using System;
using System.IO;
public class StreamOf6Bits
{
Stream baseStream;
int phase=0;
byte lastByteRead=0;
bool eof=false;
public StreamOf6Bits (Stream stream)
{
baseStream = stream;
}
/// <summary>
/// Reads 6 bits from the input, returning
/// them or -1 for end-of-stream.
/// </summary>
public int Read()
{
if (eof)
return -1;
byte nextByte=0;
if (phase != 3)
{
int x = baseStream.ReadByte();
if (x==-1)
{
eof=true;
return -1;
}
nextByte=(byte)x;
}
byte ret=0;
switch (phase)
{
case 0:
ret = (byte)(nextByte>>2);
break;
case 1:
ret = (byte)(((lastByteRead&0x3)<<4) | (nextByte>>4));
break;
case 2:
ret = (byte)(((lastByteRead&0xf)<<2) | (nextByte>>6));
break;
case 3:
ret = (byte)(lastByteRead&0x3f);
break;
default:
throw new Exception ("You should never get here");
}
phase=(phase+1)&3;
lastByteRead=nextByte;
return ret;
}
}
That's completely untested (although it does compile). It assumes that
the first 6 bits are in the high 6 bits of the first byte, then the
high 2 bits of the second set of 6 bits are in the low 2 bits of the
first byte, etc.
It's very inefficient as it's reading a single byte at a time from the
stream, but it wouldn't be too hard to fix that.