How do I convert from 8 bit byte to 7 bit byte (Base 256 to Base 128)
I am looking to do something like this:
public string BytesToString(byte[] in)
{
}
Is UTF-7 what you're looking for?
Alternatively, there's the ASCIIEncoding class that converts a UTF-8 String to an array of 8-bits bytes, discarding characters that can't be represented in 7-bits ASCII.
It's a bit difficult to determine from your question (as it currently stands) what you're trying to achieve. Are you trying to do base-128 encoding, or are you trying to conver a series of (presumably hexadecimal) digits representing 7bit numbers into the equivalent binary 8bit numbers?
The encoding I just described is the one used in the ID3v2 tag format for encoding the size field in the header.
If this is what you're trying to achieve, then perhaps something like the code below will do the trick. It's based on the '257' example in the ID3 specification:
[Test]
public void GetInt()
{
var bytes = new byte[] { 0, 0, 2, 1};
var result = 0;
foreach (var b in bytes)
{
result <<= 7;
result = result + (b & 0x7f);
}
Assert.That(result, Is.EqualTo(257));
}
[Test]
public void SetInt()
{
var i = 257;
var bytes = new Stack<byte>();
for (var j = 0 ; j < sizeof(int) ; j++)
{
var b = (byte)(i & 0x7f);
bytes.Push(b);
i >>= 7;
}
Assert.That(bytes.Pop(), Is.EqualTo(0));
Assert.That(bytes.Pop(), Is.EqualTo(0));
Assert.That(bytes.Pop(), Is.EqualTo(2));
Assert.That(bytes.Pop(), Is.EqualTo(1));
}
Base64 encodes 6 bits per character, producing a string which can be reliably transmitted with very little effort (modulo being careful about URLs).
There is no 7-bit alphabet with the same properties - many, many systems will fail if they're given control characters, for example.
Are you absolutely sure that you're not going to need to go through any such systems (including for storage)? It the extra tiny bit of space saving really enough to justify having to worry about whether something's going to change "\n" to "\r\n" or vice versa, or drop character 0?
(For a storage example, 2100 bytes = 2800 chars in base64, or 2400 chars in base128. Not a massive difference IMO.)
I'd strongly urge you to see whether you can find the extra storage space - it's likely to save a lot of headaches later.