Distribution_Service/Server/PortService/ByteTool.h
2025-12-03 18:08:23 +08:00

195 lines
6.9 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#ifndef OH_BS_BYTETOOL_H
#define OH_BS_BYTETOOL_H
#include "CCByte.h"
#include "CCString.h"
#include <chrono>
#include <cstddef>
#include <random>
#include <string>
#include <sstream>
#include <iomanip>
class APPTool {
public:
static CTL::String GetHX(CTL::Byte byte){
std::stringstream ss;
ss << "0x" << std::hex << std::uppercase << std::setw(2) << std::setfill('0') << static_cast<int>(byte.get());
const std::string hexString = ss.str();
return hexString;
}
static CTL::ByteArray GetBytes(const int value) {
const auto highByte = static_cast<CTL::Byte>((value >> 8) & 0xFF);
const auto lowByte = static_cast<CTL::Byte>(value & 0xFF);
return CTL::ByteArray{lowByte,highByte};
}
static int GetInt(const CTL::Byte byte1, const CTL::Byte byte2) {
const int lowUnsigned = byte1.get() & 0xFF;
const int highUnsigned = byte2.get() & 0xFF;
return (highUnsigned << 8) | lowUnsigned;
}
static CTL::ByteArray GetHL4(const int value) {
auto b1 = static_cast<CTL::Byte>(value & 0xFF);
auto b2 = static_cast<CTL::Byte>((value >> 8) & 0xFF);
auto b3 = static_cast<CTL::Byte>((value >> 16) & 0xFF);
auto b4 = static_cast<CTL::Byte>((value >> 24) & 0xFF);
return {b1, b2, b3, b4};
}
static int GetInt(const CTL::Byte low1,const CTL::Byte low2,const CTL::Byte high1,const CTL::Byte high2) {
const int lowUnsigned = (low1.get() & 0xFF) | ((low2.get() & 0xFF) << 8);
const int highUnsigned = (high1.get() & 0xFF) | ((high2.get() & 0xFF) << 8);
return (highUnsigned << 16) | lowUnsigned;
}
static CTL::String GenerateVerificationCode(){
// 1. 获取微秒级时间戳 (18-19位)
auto now = std::chrono::system_clock::now();
auto micros = std::chrono::duration_cast<std::chrono::microseconds>(
now.time_since_epoch()
).count();
// 2. 初始化随机数引擎 (使用时间戳+随机设备双种子)
std::random_device rd;
std::mt19937_64 rng(micros + rd());
// 3. 定义验证码字符集 (62个字符)
const std::string charset =
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
// 4. 生成16位随机验证码
std::uniform_int_distribution<size_t> dist(0, charset.size() - 1);
std::string code;
for (int i = 0; i < 16; ++i) {
code += charset[dist(rng)];
}
return code;
}
static size_t GetSize(const CTL::Byte byte1, const CTL::Byte byte2, const CTL::Byte byte3,const CTL::Byte byte4){
return (byte1.get() << 24) | (byte2.get() << 16) | (byte3.get() << 8) | byte4.get();
}
static CTL::ByteArray GetBytesSize(const int value) {
const auto byte1 = static_cast<CTL::Byte>((value >> 24) & 0xFF);
const auto byte2 = static_cast<CTL::Byte>((value >> 16) & 0xFF);
const auto byte3 = static_cast<CTL::Byte>((value >> 8) & 0xFF);
const auto byte4 = static_cast<CTL::Byte>(value & 0xFF);
return {byte1, byte2, byte3, byte4};
}
static CTL::ByteArray GetIPBytes(const CTL::String& IP) {
CTL::ByteArray bytes;
bytes.resize(4);
std::stringstream ss(IP);
std::string segment;
int index = 3;
while (std::getline(ss, segment, '.') && index >= 0) {
const int value = std::stoi(segment);
bytes.set(index,static_cast<CTL::Byte>(value & 0xFF));
index--;
}
if (index != -1) {
throw std::invalid_argument("Invalid IP address format");
}
return bytes;
}
static std::vector<int> GetTermList(const CTL::ByteArray& TM) {
std::vector<int> Tid;
for (size_t i = 0; i < TM.size(); i++) {
CTL::Byte currentByte = TM.get(i);
for (int j = 0; j < 8; j++) {
const int bitIndex = static_cast<int>(i) * 8 + j;
if ((currentByte.get() & (1 << j)) != 0) {
Tid.push_back(bitIndex + 1);
}
}
}
return Tid;
}
static CTL::ByteArray SetTermList(const std::vector<int>& Tid, size_t byteSize) {
// 创建指定大小的字节数组初始值为0
CTL::ByteArray TM(byteSize);
// 遍历所有需要设置的位索引
for (int tid : Tid) {
// 转换为0基索引
int index = tid - 1;
// 计算字节位置和位位置
size_t byteIndex = index / 8;
int bitOffset = index % 8;
// 检查索引是否有效
if (byteIndex < byteSize) {
// 设置对应位为1
CTL::Byte currentByte = TM.get(byteIndex);
currentByte.set(currentByte.get() | (1 << bitOffset));
TM.set(byteIndex, currentByte);
}
}
return TM;
}
static CTL::ByteArray intToBytes(const int value) {
// 将整数格式化为4位十六进制字符串大写
char hexString[5]; // 4个字符 + 1个结束符
sprintf(hexString, "%04X", value & 0xFFFF); // 限制为16位
// 创建ByteArray
CTL::ByteArray bytes;
bytes.resize(4);
// 将字符串的每个字符转换为Byte
for (int i = 0; i < 4; i++) {
bytes.set(i, CTL::Byte(static_cast<unsigned char>(hexString[i])));
}
return bytes;
}
static uint8_t Get8Bit(const CCVector<uint8_t>& bytes) {
// 检查输入数组大小
if (bytes.size() != 8) {
throw std::invalid_argument("Input vector must contain exactly 8 elements");
}
uint8_t result = 0;
for (size_t i = 0; i < 8; ++i) {
// 确保数组中的值只能是0或1
if (bytes[i] != 0 && bytes[i] != 1) {
throw std::invalid_argument("Vector elements must be 0 or 1");
}
// 将每个位设置到结果字节中
// 从最高位开始设置索引0对应最高位
if (bytes[i] == 1) {
result |= (1 << (7 - i));
}
}
return result;
}
static CCVector<uint8_t> Get8toByte(const uint8_t value) {
CCVector<uint8_t> bytes;
bytes.resize(8);
// 从最高位到最低位提取每一位
for (int i = 0; i < 8; ++i) {
// 检查第(7-i)位是否为1
if (value & (1 << (7 - i))) {
bytes[i] = 1;
}
else {
bytes[i] = 0;
}
}
return bytes;
}
static uint8_t ReverseBits(uint8_t value) {
// 交换相邻的位
value = ((value & 0xAA) >> 1) | ((value & 0x55) << 1);
// 交换相邻的2位组
value = ((value & 0xCC) >> 2) | ((value & 0x33) << 2);
// 交换相邻的4位组
value = (value >> 4) | (value << 4);
return value;
}
};
#endif