关于base2048
  • 板块学术版
  • 楼主Caviar_X
  • 当前回复12
  • 已保存回复12
  • 发布时间2021/10/18 20:08
  • 上次更新2023/11/4 03:22:00
查看原帖
关于base2048
278124
Caviar_X楼主2021/10/18 20:08

鄙人最近在钻研base2048,写出了如下代码

tables.hpp放在这里

base2048.cpp

#include "tables.hpp"
#include <bits/stdc++.h>
using namespace std;
size_t unsigned_strlen(unsigned char *s)
{
    unsigned char *tmp = s;
    assert(s);
    while (*s++ != '\0')
        ;
    return s - tmp - 1;
}
wstring encode(unsigned char *bytes)
{
    wstring ret = L"";
    uint16_t stage = 0x000;
    int remaining = 0;
    for (int i = 0; i < unsigned_strlen(bytes); i++)
    {
        uint16_t byte = bytes[i];
        int need = 11 - remaining;
        if (need <= 8)
        {
            remaining = 8 - need;
            uint16_t index = (stage << need) | (byte >> remaining);
            ret.push_back(ENC_TABLE[index]);
            stage = byte & (1 << remaining - 1);
        }
        else
        {
            stage = (stage << 8) | byte;
            remaining += 8;
        }
    }
    if (remaining > 0)
    {
        if (remaining <= 3)
        {
            ret.push_back(TAIL[stage]);
        }
        else
        {
            ret.push_back(ENC_TABLE[stage]);
        }
    }
    return ret;
}

main.cpp

#include <bits/stdc++.h>
#include "base2048.cpp"
using namespace std;
int main()
{
    wcout << encode((unsigned char *)"some utf8 bytes to encode more compactly");
}

预期输出

өПәΰศƪڍথԅПяޒߡೲױɡјවၕάێנלۻӥ༟ಖ༎

实际输出

?????y??????s????????a????????

是因为字符没有对齐吗,求大佬解惑

2021/10/18 20:08
加载中...