简体   繁体   中英

How do I convert from 8 bit byte to 7 bit byte (Base 256 to Base 128)

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)
{

}

public byte[] StringToBytes(string in)
{

}

I know base64 is available but it expands a byte array too much.

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.

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));
}

您正在寻找UTF-7吗?

另外,还有ASCIIEncoding类,它将utf-8字符串转换为8位字节的数组,丢弃无法用7位ASCII表示的字符。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM