//读数据:
vector<uint8_t> pData;
wMCom1->readData(pData, 3);
//写数据:
std::vector<uint8_t> sData = {0x12, 0x34, 0xAB, 0xCD};
wMCom1->writeData(sData);
1)16进制显示
string Hexstring = vectorToHexString(pData);
vm->setChar("显示数据", Hexstring);
2)原生字符串显示
string str1 = StringUtil::charArrayToString(pData);
vm->setChar("显示数据", Hexstring);
// 示例 1: vector<uint8_t> 转字符串
std::vector<uint8_t> arr = { 'H', 'e', 'l', 'l', 'o' };
std::string str1 = StringUtil::charArrayToString(arr);
// 示例 2: 字符串转 vector<uint8_t>
std::string srcStr = "World";
std::vector<uint8_t> arr2 = StringUtil::stringToCharArray(srcStr);
std::cout << "字符串转 vector<uint8_t>: ";
for (uint8_t c : arr2) {
std::cout << static_cast<char>(c);
}
std::cout << std::endl;
#include <sstream>
#include <iomanip>
// 将 vector<uint8_t> 转换为十六进制字符串
std::string vectorToHexString(const std::vector<uint8_t>& pData) {
std::ostringstream oss;
oss << std::hex << std::setfill('0');
for (uint8_t byte : pData) {
oss << std::setw(2) << static_cast<int>(byte);
}
return oss.str();
}
1)vecto<uint8_t>与整形之间的转换
// 将 vector<uint8_t> 中指定位置开始的 4 字节数据转换为 uint32_t
int vectorToUint32(const std::vector<uint8_t>& pData, size_t startIndex) {
if (startIndex + 4 > pData.size()) {
return 0;
}
int result = 0;
for (int i = 0; i < 4; ++i) {
result |= static_cast<int>(pData[startIndex + i]) << ((3 - i) * 8);
}
return result;
}
// 将 uint32_t 写入 vector<uint8_t> 中指定位置
void uint32ToVector(std::vector<uint8_t>& pData, int num, size_t startIndex) {
if (startIndex + 4 > pData.size()) {
pData.resize(startIndex + 4);
}
for (int i = 0; i < 4; ++i) {
pData[startIndex + i] = static_cast<uint8_t>((num >> ((3 - i) * 8)) & 0xFF);
}
}
//使用方法
// 示例 vector<uint8_t>
std::vector<uint8_t> pData = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88};
// 从索引 2 开始转换为 uint32_t
uint32_t num = vectorToUint32(pData, 2);
std::cout << "Converted uint32_t (starting from index 2): 0x" << std::hex << num << std::endl;
// 将 uint32_t 写入索引 4 的位置
uint32_t testNum = 0xABCD1234;
uint32ToVector(pData, testNum, 4);
// 输出更新后的 vector<uint8_t>
std::cout << "Updated vector<uint8_t>: ";
for (uint8_t byte : pData) {
std::cout << std::hex << static_cast<int>(byte) << " ";
}
std::cout << std::endl;
2)vecto<uint8_t>与浮点之间的转换
// 将 vector<uint8_t> 中指定位置开始的 4 字节数据转换为 float
float vectorToFloat(const std::vector<uint8_t>& pData, size_t startIndex) {
if (startIndex + sizeof(float) > pData.size()) {
std::cerr << "Not enough data in the vector starting from index " << startIndex << std::endl;
return 0.0f;
}
uint32_t rawValue = 0;
for (int i = 0; i < sizeof(float); ++i) {
rawValue |= static_cast<uint32_t>(pData[startIndex + i]) << ((sizeof(float) - 1 - i) * 8);
}
float result;
std::memcpy(&result, &rawValue, sizeof(float));
return result;
}
// 将 float 写入 vector<uint8_t> 中指定位置
void floatToVector(std::vector<uint8_t>& pData, float num, size_t startIndex) {
if (startIndex + sizeof(float) > pData.size()) {
pData.resize(startIndex + sizeof(float));
}
uint32_t rawValue;
std::memcpy(&rawValue, &num, sizeof(float));
for (int i = 0; i < sizeof(float); ++i) {
pData[startIndex + i] = static_cast<uint8_t>((rawValue >> ((sizeof(float) - 1 - i) * 8)) & 0xFF);
}
}
//使用方法
// 示例 vector<uint8_t>
std::vector<uint8_t> pData = {0x00, 0x00, 0x80, 0x3F, 0x00, 0x00, 0x00, 0x00};
// 从索引 0 开始转换为 float
float num = vectorToFloat(pData, 0);
std::cout << "Converted float (starting from index 0): " << num << std::endl;
// 将 float 写入索引 4 的位置
float testNum = 3.14f;
floatToVector(pData, testNum, 4);
// 输出更新后的 vector<uint8_t>
std::cout << "Updated vector<uint8_t>: ";
for (uint8_t byte : pData) {
std::cout << std::hex << static_cast<int>(byte) << " ";
}
std::cout << std::endl;
3)vecto<uint8_t>与long之间的转换
// 将 vector<uint8_t> 中指定位置开始的 8 字节数据转换为 long
long vectorToLong(const std::vector<uint8_t>& pData, size_t startIndex) {
if (startIndex + sizeof(long) > pData.size()) {
std::cerr << "Not enough data in the vector starting from index " << startIndex << std::endl;
return 0;
}
long result = 0;
for (int i = 0; i < sizeof(long); ++i) {
result |= static_cast<long>(pData[startIndex + i]) << ((sizeof(long) - 1 - i) * 8);
}
return result;
}
// 将 long 写入 vector<uint8_t> 中指定位置
void longToVector(std::vector<uint8_t>& pData, long num, size_t startIndex) {
if (startIndex + sizeof(long) > pData.size()) {
pData.resize(startIndex + sizeof(long));
}
for (int i = 0; i < sizeof(long); ++i) {
pData[startIndex + i] = static_cast<uint8_t>((num >> ((sizeof(long) - 1 - i) * 8)) & 0xFF);
}
}
// 示例 vector<uint8_t>
std::vector<uint8_t> pData = {0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
// 从索引 0 开始转换为 long
long num = vectorToLong(pData, 0);
std::cout << "Converted long (starting from index 0): " << num << std::endl;
// 将 long 写入索引 8 的位置
long testNum = 1234567890L;
longToVector(pData, testNum, 8);
// 输出更新后的 vector<uint8_t>
std::cout << "Updated vector<uint8_t>: ";
for (uint8_t byte : pData) {
std::cout << std::hex << static_cast<int>(byte) << " ";
}
std::cout << std::endl;