mirror of
https://github.com/zs-yg/kortapp-z.git
synced 2025-12-06 16:10:42 +08:00
添加文本转换器代码
This commit is contained in:
37
others/C++/text converter/src/Base32Converter.cpp
Normal file
37
others/C++/text converter/src/Base32Converter.cpp
Normal file
@@ -0,0 +1,37 @@
|
||||
#include "../include/Base32Converter.hpp"
|
||||
#include <string>
|
||||
#include <stdexcept>
|
||||
|
||||
const std::string BASE32_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
|
||||
|
||||
std::string Base32Converter::convert(const std::string& input) {
|
||||
std::string result;
|
||||
int buffer = 0;
|
||||
int bitsLeft = 0;
|
||||
int count = 0;
|
||||
|
||||
for (unsigned char c : input) {
|
||||
buffer <<= 8;
|
||||
buffer |= c;
|
||||
bitsLeft += 8;
|
||||
count++;
|
||||
|
||||
while (bitsLeft >= 5) {
|
||||
int index = (buffer >> (bitsLeft - 5)) & 0x1F;
|
||||
result += BASE32_CHARS[index];
|
||||
bitsLeft -= 5;
|
||||
}
|
||||
}
|
||||
|
||||
if (bitsLeft > 0) {
|
||||
int index = (buffer << (5 - bitsLeft)) & 0x1F;
|
||||
result += BASE32_CHARS[index];
|
||||
}
|
||||
|
||||
// 添加填充字符
|
||||
while (result.size() % 8 != 0) {
|
||||
result += '=';
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
Reference in New Issue
Block a user