提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
使用C++将进制转换封装成类,直接调用:
部分代码是网上找到的并加以总结。其中十六转十和十转十六,来不及写,留个案例即可按照思路写即可。
AOfBaseToBOfBase.h文件
#pragma once
#include
#include
#include
#include
using namespace std;
class AOfBaseToBOfBase
{
public:
AOfBaseToBOfBase();
~AOfBaseToBOfBase();
//十六进制转字符串
string hexToString(const unsigned char*, short);
//字符串转十六进制
bool stringToHex(const unsigned char * pAscii, unsigned char * pHex, short nLen);
//十六进制转二进制
void hexToBinStr(const unsigned char* hexStr, unsigned char* binStr, int lenHex);
//二进制转十六进制
void binStrToHex(const unsigned char* binStr, unsigned char* hexStr, int lenBin);
void hexToDoc();
//CRC校验
uint16_t crc16_update(uint16_t crc, uint8_t a);
uint16_t getcrc(uint8_t *data, int nbytes);
//LRC校验
unsigned char get_LRC(uint8_t *data, uint8_t len);
string printHexData(unsigned char* data, int len);
};
AOfBaseToBOfBase.cpp文件
#include "AofBaseToBofBase.h"
AOfBaseToBOfBase::AOfBaseToBOfBase()
{
}
AOfBaseToBOfBase::~AOfBaseToBOfBase()
{
}
string AOfBaseToBOfBase::hexToString( const unsigned char *hexStr , short len)
{
string ret;
string binaryStr;
for (int i = 0; i < len; ++i) {
binaryStr.push_back(*(hexStr+i));
}
static const char *hex = "0123456789ABCDEF";
for (auto c : binaryStr)
{
ret.push_back(hex[(c >> 4) & 0x0f]); //取二进制高四位
ret.push_back(hex[c & 0x0f]); //取二进制低四位
}
return ret;
}
bool AOfBaseToBOfBase::stringToHex(const unsigned char * pAscii, unsigned char * pHex, short nLen)
{
if (nLen % 2)// 十六进制必须是偶字节长度
return false;
int nHexLen = nLen / 2;
for (int i = 0; i < nHexLen; i++) //,每次处理两个字节
{
uint8_t Nibble[2];
Nibble[0] = *pAscii++;
Nibble[1] = *pAscii++;
for (int j = 0; j < 2; j++)
{
if (Nibble[j] <= 'F' && Nibble[j] >= 'A')
Nibble[j] = Nibble[j] - 'A' + 10;
else if (Nibble[j] <= 'f' && Nibble[j] >= 'a')
Nibble[j] = Nibble[j] - 'a' + 10;
else if (Nibble[j] >= '0' && Nibble[j] <= '9')
Nibble[j] = Nibble[j] - '0';
else
return false;
} // for (int j = ...)
pHex[i] = Nibble[0] << 4; // Set the high nibble
pHex[i] |= Nibble[1]; //Set the low nibble
} // for (int i = ...)
return true;
}
/***十六进制数转换成二进制字符串***/
void AOfBaseToBOfBase::hexToBinStr(const unsigned char* hexStr, unsigned char* binStr, int lenHex)
{
memset(binStr, '0', lenHex * 8);
unsigned char hexChar[2];
for (int i = 0; i < lenHex; i++)
{
hexChar[0] = (hexStr[i] & 0xF0) >> 4;
hexChar[1] = hexStr[i] & 0x0F;
for (int j = 0; j < 2; j++)
{
for (int k = 0; k < 4; k++)
{
if (hexChar[j] & (0x08 >> k))
{
binStr[8 * i + 4 * j + k] = '1';
}
}
}
}
}
/***二进制字符串转换成十六进制数***/
void AOfBaseToBOfBase::binStrToHex(const unsigned char* binStr, unsigned char* hexStr, int lenBin)
{
int lenHex = lenBin / 8;
memset(hexStr, '\0', lenHex);
unsigned char hexChar[2];
for (int i = 0; i < lenHex; i++)
{
for (int j = 0; j < 2; j++)
{
hexChar[j] = 0;
for (int k = 0; k < 4; k++)
{
if (binStr[8 * i + 4 * j + k] == '1')
{
hexChar[j] |= (0x08 >> k);
}
}
}
hexStr[i] = ((hexChar[0] & 0x0F) << 4) | (hexChar[1] & 0x0F);
}
}
void AOfBaseToBOfBase::hexToDoc() {
int data = 255;
uint8_t hexData = (uint8_t)data & 0xFF;
int h = hexData >> 4 & 0x0f;
int l = hexData & 0x0f;
cout << h * pow(16, 1) + l << endl;
}
uint16_t AOfBaseToBOfBase::crc16_update(uint16_t crc, uint8_t a) {
int i;
crc ^= a;
for (i = 0; i < 8; ++i) {
if (crc & 1)
crc = (crc >> 1) ^ 0xA001;
else
crc = (crc >> 1);
}
return crc;
}
uint16_t AOfBaseToBOfBase::getcrc(uint8_t* data,int nbytes)
{
int i;
uint16_t crc = 0;
for (i = 0; i < nbytes; i++) {
crc = crc16_update(crc, data[i]);
}
return crc;
}
unsigned char AOfBaseToBOfBase::get_LRC(uint8_t *data, uint8_t len) {
unsigned char uchLRC = 0;
while (len--)
{
uchLRC += *data++;
}
return ((unsigned char)(-((char)uchLRC)));
}
/*
std::uppercase:可以将输入转为大写
std::hex:将输入按照16进制的方式展示出来
std::setfilll(‘0’) 当不足要求长度的时候填充’0’来满足长度要求
std::setw(2) 两个字节的长度输出
*/
string AOfBaseToBOfBase::printHexData(unsigned char* data, int len) {
stringstream ss;
ss << std::uppercase << std::hex << std::setfill('0');
for (int i = 0; i < len; i++) {
ss << std::setw(2) << static_cast<unsigned>(data[i]);
}
return ss.str();
}
拷贝下来直接用。