mirror of
https://github.com/zs-yg/kortapp-z.git
synced 2025-12-06 16:10:42 +08:00
添加文本转换器代码
This commit is contained in:
47
others/C++/text converter/src/Ascii85Converter.cpp
Normal file
47
others/C++/text converter/src/Ascii85Converter.cpp
Normal file
@@ -0,0 +1,47 @@
|
||||
#include "../include/Ascii85Converter.hpp"
|
||||
#include <string>
|
||||
#include <cmath>
|
||||
#include <sstream>
|
||||
#include <cstdint>
|
||||
|
||||
const std::string ASCII85_PREFIX = "<~";
|
||||
const std::string ASCII85_SUFFIX = "~>";
|
||||
|
||||
std::string Ascii85Converter::convert(const std::string& input) {
|
||||
std::stringstream result;
|
||||
result << ASCII85_PREFIX;
|
||||
|
||||
size_t i = 0;
|
||||
while (i < input.size()) {
|
||||
uint32_t value = 0;
|
||||
int bytes = 0;
|
||||
|
||||
// 读取4个字节
|
||||
for (int j = 0; j < 4 && (i + j) < input.size(); j++) {
|
||||
value = (value << 8) | static_cast<unsigned char>(input[i + j]);
|
||||
bytes++;
|
||||
}
|
||||
i += bytes;
|
||||
|
||||
// 特殊处理全0的4字节
|
||||
if (value == 0 && bytes == 4) {
|
||||
result << 'z';
|
||||
continue;
|
||||
}
|
||||
|
||||
// 转换为Ascii85
|
||||
char chars[5];
|
||||
for (int j = 4; j >= 0; j--) {
|
||||
chars[j] = value % 85 + '!';
|
||||
value /= 85;
|
||||
}
|
||||
|
||||
// 写入结果(只写入bytes+1个字符)
|
||||
for (int j = 0; j < bytes + 1; j++) {
|
||||
result << chars[j];
|
||||
}
|
||||
}
|
||||
|
||||
result << ASCII85_SUFFIX;
|
||||
return result.str();
|
||||
}
|
||||
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;
|
||||
}
|
||||
22
others/C++/text converter/src/Base64Converter.cpp
Normal file
22
others/C++/text converter/src/Base64Converter.cpp
Normal file
@@ -0,0 +1,22 @@
|
||||
#include "../include/Base64Converter.hpp"
|
||||
#include <openssl/bio.h>
|
||||
#include <openssl/evp.h>
|
||||
#include <string>
|
||||
|
||||
std::string Base64Converter::convert(const std::string& input) {
|
||||
// Base64编码
|
||||
BIO *b64 = BIO_new(BIO_f_base64());
|
||||
BIO *bio = BIO_new(BIO_s_mem());
|
||||
BIO_push(b64, bio);
|
||||
BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
|
||||
|
||||
BIO_write(b64, input.c_str(), input.length());
|
||||
BIO_flush(b64);
|
||||
|
||||
char* buffer;
|
||||
long length = BIO_get_mem_data(bio, &buffer);
|
||||
std::string result(buffer, length);
|
||||
|
||||
BIO_free_all(b64);
|
||||
return result;
|
||||
}
|
||||
27
others/C++/text converter/src/BinaryConverter.cpp
Normal file
27
others/C++/text converter/src/BinaryConverter.cpp
Normal file
@@ -0,0 +1,27 @@
|
||||
#include "../include/BinaryConverter.hpp"
|
||||
#include <bitset>
|
||||
#include <sstream>
|
||||
#include <algorithm>
|
||||
|
||||
std::string BinaryConverter::convert(const std::string& input) {
|
||||
if (input.empty()) {
|
||||
return "";
|
||||
}
|
||||
|
||||
std::stringstream result;
|
||||
for (char c : input) {
|
||||
std::string binary = std::bitset<8>(c).to_string();
|
||||
// 去除前导0,保留后6位
|
||||
size_t firstOne = binary.find('1');
|
||||
if (firstOne != std::string::npos) {
|
||||
binary = binary.substr(firstOne);
|
||||
}
|
||||
result << binary << " ";
|
||||
}
|
||||
|
||||
std::string output = result.str();
|
||||
if (!output.empty()) {
|
||||
output.pop_back(); // 移除最后一个空格
|
||||
}
|
||||
return output;
|
||||
}
|
||||
28
others/C++/text converter/src/CRC32Converter.cpp
Normal file
28
others/C++/text converter/src/CRC32Converter.cpp
Normal file
@@ -0,0 +1,28 @@
|
||||
#include "../include/CRC32Converter.hpp"
|
||||
#include <string>
|
||||
#include <sstream>
|
||||
#include <iomanip>
|
||||
#include <cstdint>
|
||||
|
||||
// CRC32多项式
|
||||
const uint32_t CRC32_POLY = 0xEDB88320;
|
||||
|
||||
uint32_t computeCRC32(const std::string& input) {
|
||||
uint32_t crc = 0xFFFFFFFF;
|
||||
|
||||
for (char c : input) {
|
||||
crc ^= static_cast<unsigned char>(c);
|
||||
for (int i = 0; i < 8; i++) {
|
||||
crc = (crc >> 1) ^ ((crc & 1) ? CRC32_POLY : 0);
|
||||
}
|
||||
}
|
||||
|
||||
return ~crc;
|
||||
}
|
||||
|
||||
std::string CRC32Converter::convert(const std::string& input) {
|
||||
uint32_t crc = computeCRC32(input);
|
||||
std::stringstream ss;
|
||||
ss << std::hex << std::setw(8) << std::setfill('0') << crc;
|
||||
return ss.str();
|
||||
}
|
||||
23
others/C++/text converter/src/HexConverter.cpp
Normal file
23
others/C++/text converter/src/HexConverter.cpp
Normal file
@@ -0,0 +1,23 @@
|
||||
#include "../include/HexConverter.hpp"
|
||||
#include <iomanip>
|
||||
#include <sstream>
|
||||
|
||||
std::string HexConverter::convert(const std::string& input) {
|
||||
if (input.empty()) {
|
||||
return "";
|
||||
}
|
||||
|
||||
std::stringstream result;
|
||||
for (char c : input) {
|
||||
result << std::hex << std::setw(2) << std::setfill('0')
|
||||
<< static_cast<int>(static_cast<unsigned char>(c)) << " ";
|
||||
}
|
||||
|
||||
std::string output = result.str();
|
||||
// 移除最后一个空格
|
||||
if (!output.empty()) {
|
||||
output.pop_back();
|
||||
}
|
||||
|
||||
return output;
|
||||
}
|
||||
23
others/C++/text converter/src/MD5Converter.cpp
Normal file
23
others/C++/text converter/src/MD5Converter.cpp
Normal file
@@ -0,0 +1,23 @@
|
||||
#include "../include/MD5Converter.hpp"
|
||||
#include <openssl/evp.h>
|
||||
#include <sstream>
|
||||
#include <iomanip>
|
||||
|
||||
std::string MD5Converter::convert(const std::string& input) {
|
||||
EVP_MD_CTX* mdctx = EVP_MD_CTX_new();
|
||||
const EVP_MD* md = EVP_md5();
|
||||
unsigned char digest[EVP_MAX_MD_SIZE];
|
||||
unsigned int digest_len;
|
||||
|
||||
EVP_DigestInit_ex(mdctx, md, nullptr);
|
||||
EVP_DigestUpdate(mdctx, input.c_str(), input.length());
|
||||
EVP_DigestFinal_ex(mdctx, digest, &digest_len);
|
||||
EVP_MD_CTX_free(mdctx);
|
||||
|
||||
std::stringstream ss;
|
||||
for(unsigned int i = 0; i < digest_len; i++) {
|
||||
ss << std::hex << std::setw(2) << std::setfill('0') << (int)digest[i];
|
||||
}
|
||||
|
||||
return ss.str();
|
||||
}
|
||||
14
others/C++/text converter/src/Main.cpp
Normal file
14
others/C++/text converter/src/Main.cpp
Normal file
@@ -0,0 +1,14 @@
|
||||
#include "../include/MainWindow.hpp"
|
||||
#include "../include/Config.hpp"
|
||||
#include <FL/Fl.H>
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
// 创建主窗口
|
||||
MainWindow window(Config::WINDOW_WIDTH, Config::WINDOW_HEIGHT, Config::WINDOW_TITLE);
|
||||
|
||||
// 显示窗口
|
||||
window.show(argc, argv);
|
||||
|
||||
// 运行FLTK主循环
|
||||
return Fl::run();
|
||||
}
|
||||
66
others/C++/text converter/src/MainWindow.cpp
Normal file
66
others/C++/text converter/src/MainWindow.cpp
Normal file
@@ -0,0 +1,66 @@
|
||||
#include "../include/MainWindow.hpp"
|
||||
#include "../include/Utils.hpp"
|
||||
#include "../include/Config.hpp"
|
||||
#include <FL/Fl_Box.H>
|
||||
#include <FL/fl_ask.H>
|
||||
|
||||
MainWindow::MainWindow(int w, int h, const char* title) : Fl_Window(w, h, title) {
|
||||
// 初始化UI元素
|
||||
inputText = new Fl_Input(Config::INPUT_X, Config::INPUT_Y,
|
||||
Config::INPUT_WIDTH, Config::INPUT_HEIGHT, "输入文本:");
|
||||
outputText = new Fl_Output(Config::OUTPUT_X, Config::OUTPUT_Y,
|
||||
Config::OUTPUT_WIDTH, Config::OUTPUT_HEIGHT, "输出结果:");
|
||||
conversionType = new Fl_Choice(Config::CHOICE_X, Config::CHOICE_Y,
|
||||
Config::CHOICE_WIDTH, Config::CHOICE_HEIGHT, "转换类型:");
|
||||
convertButton = new Fl_Button(Config::BUTTON_X, Config::BUTTON_Y,
|
||||
Config::BUTTON_WIDTH, Config::BUTTON_HEIGHT, "转换");
|
||||
|
||||
// 设置转换类型选项
|
||||
conversionType->add("二进制");
|
||||
conversionType->add("十六进制");
|
||||
conversionType->add("ROT13");
|
||||
conversionType->add("MD5");
|
||||
conversionType->add("SHA1");
|
||||
conversionType->add("SHA256");
|
||||
conversionType->add("Base64");
|
||||
conversionType->add("Base32");
|
||||
conversionType->add("Ascii85");
|
||||
conversionType->add("CRC32");
|
||||
conversionType->value(0); // 默认选择二进制
|
||||
|
||||
// 设置按钮回调
|
||||
convertButton->callback(ConvertCallback, this);
|
||||
|
||||
end(); // 结束窗口组件添加
|
||||
}
|
||||
|
||||
MainWindow::~MainWindow() {
|
||||
// 清理资源
|
||||
delete inputText;
|
||||
delete outputText;
|
||||
delete conversionType;
|
||||
delete convertButton;
|
||||
}
|
||||
|
||||
void MainWindow::ConvertCallback(Fl_Widget* widget, void* data) {
|
||||
MainWindow* window = static_cast<MainWindow*>(data);
|
||||
window->ConvertText();
|
||||
}
|
||||
|
||||
void MainWindow::ConvertText() {
|
||||
const char* input = inputText->value();
|
||||
if (!input || strlen(input) == 0) {
|
||||
fl_alert("请输入要转换的文本");
|
||||
return;
|
||||
}
|
||||
|
||||
int type = conversionType->value();
|
||||
auto converter = Utils::createConverter(type);
|
||||
if (!converter) {
|
||||
fl_alert("不支持的转换类型");
|
||||
return;
|
||||
}
|
||||
|
||||
std::string result = converter->convert(input);
|
||||
outputText->value(result.c_str());
|
||||
}
|
||||
13
others/C++/text converter/src/ROT13Converter.cpp
Normal file
13
others/C++/text converter/src/ROT13Converter.cpp
Normal file
@@ -0,0 +1,13 @@
|
||||
#include "../include/ROT13Converter.hpp"
|
||||
|
||||
std::string ROT13Converter::convert(const std::string& input) {
|
||||
std::string result;
|
||||
for (char c : input) {
|
||||
if (isalpha(c)) {
|
||||
char base = isupper(c) ? 'A' : 'a';
|
||||
c = (c - base + 13) % 26 + base;
|
||||
}
|
||||
result += c;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
23
others/C++/text converter/src/SHA1Converter.cpp
Normal file
23
others/C++/text converter/src/SHA1Converter.cpp
Normal file
@@ -0,0 +1,23 @@
|
||||
#include "../include/SHA1Converter.hpp"
|
||||
#include <openssl/evp.h>
|
||||
#include <sstream>
|
||||
#include <iomanip>
|
||||
|
||||
std::string SHA1Converter::convert(const std::string& input) {
|
||||
EVP_MD_CTX* mdctx = EVP_MD_CTX_new();
|
||||
const EVP_MD* md = EVP_sha1();
|
||||
unsigned char digest[EVP_MAX_MD_SIZE];
|
||||
unsigned int digest_len;
|
||||
|
||||
EVP_DigestInit_ex(mdctx, md, nullptr);
|
||||
EVP_DigestUpdate(mdctx, input.c_str(), input.length());
|
||||
EVP_DigestFinal_ex(mdctx, digest, &digest_len);
|
||||
EVP_MD_CTX_free(mdctx);
|
||||
|
||||
std::stringstream ss;
|
||||
for(unsigned int i = 0; i < digest_len; i++) {
|
||||
ss << std::hex << std::setw(2) << std::setfill('0') << (int)digest[i];
|
||||
}
|
||||
|
||||
return ss.str();
|
||||
}
|
||||
23
others/C++/text converter/src/SHA256Converter.cpp
Normal file
23
others/C++/text converter/src/SHA256Converter.cpp
Normal file
@@ -0,0 +1,23 @@
|
||||
#include "../include/SHA256Converter.hpp"
|
||||
#include <openssl/evp.h>
|
||||
#include <sstream>
|
||||
#include <iomanip>
|
||||
|
||||
std::string SHA256Converter::convert(const std::string& input) {
|
||||
EVP_MD_CTX* mdctx = EVP_MD_CTX_new();
|
||||
const EVP_MD* md = EVP_sha256();
|
||||
unsigned char digest[EVP_MAX_MD_SIZE];
|
||||
unsigned int digest_len;
|
||||
|
||||
EVP_DigestInit_ex(mdctx, md, nullptr);
|
||||
EVP_DigestUpdate(mdctx, input.c_str(), input.length());
|
||||
EVP_DigestFinal_ex(mdctx, digest, &digest_len);
|
||||
EVP_MD_CTX_free(mdctx);
|
||||
|
||||
std::stringstream ss;
|
||||
for(unsigned int i = 0; i < digest_len; i++) {
|
||||
ss << std::hex << std::setw(2) << std::setfill('0') << (int)digest[i];
|
||||
}
|
||||
|
||||
return ss.str();
|
||||
}
|
||||
25
others/C++/text converter/src/Utils.cpp
Normal file
25
others/C++/text converter/src/Utils.cpp
Normal file
@@ -0,0 +1,25 @@
|
||||
#include "../include/Utils.hpp"
|
||||
#include "../include/ROT13Converter.hpp"
|
||||
#include "../include/MD5Converter.hpp"
|
||||
#include "../include/SHA1Converter.hpp"
|
||||
#include "../include/SHA256Converter.hpp"
|
||||
#include "../include/Base64Converter.hpp"
|
||||
#include "../include/Base32Converter.hpp"
|
||||
#include "../include/Ascii85Converter.hpp"
|
||||
#include "../include/CRC32Converter.hpp"
|
||||
|
||||
std::unique_ptr<Converter> Utils::createConverter(int type) {
|
||||
switch (type) {
|
||||
case 0: return std::unique_ptr<Converter>(new BinaryConverter());
|
||||
case 1: return std::unique_ptr<Converter>(new HexConverter());
|
||||
case 2: return std::unique_ptr<Converter>(new ROT13Converter());
|
||||
case 3: return std::unique_ptr<Converter>(new MD5Converter());
|
||||
case 4: return std::unique_ptr<Converter>(new SHA1Converter());
|
||||
case 5: return std::unique_ptr<Converter>(new SHA256Converter());
|
||||
case 6: return std::unique_ptr<Converter>(new Base64Converter());
|
||||
case 7: return std::unique_ptr<Converter>(new Base32Converter());
|
||||
case 8: return std::unique_ptr<Converter>(new Ascii85Converter());
|
||||
case 9: return std::unique_ptr<Converter>(new CRC32Converter());
|
||||
default: return nullptr;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user