鄙人最近在钻研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????????
是因为字符没有对齐吗,求大佬解惑