c++ 值转换

时间:2023-03-09 05:49:09
c++ 值转换

1.double,float 四舍五入,保留小数位数。

void MainWindow::on_pushButton_clicked()
{
double number=3.141592;
double result=MyRound(number,);
qDebug()<<result;//3.142
}
#include <iostream>
#include <sstream>
#include <iomanip>

double MainWindow::myRound(double number, unsigned int bits)
{
std::stringstream ss;
ss << std::fixed << std::setprecision(bits) << number;
ss >> number;
return number;
}

2.std::to_string 数字转换成string,C++11才支持此函数,转换后小数位数是6位,无法控制小数保留位数。使用的时候可以先四舍五入后,再使用to_string 转换字符串,截取。

 float number=3.14;
std::string str=std::to_string(number);//3.140000
qDebug()<<QString::fromStdString(str);

获得指定保留小数位数的字符串

void MainWindow::on_pushButton_clicked()
{
float number=3.141592;
number=MyRound(number,);//3.142
std::string str=std::to_string(number);//3.142000
str=str.substr(,str.find('.')+);//"3.142"
qDebug()<<QString::fromStdString(str);
} float MainWindow::MyRound(float number,unsigned int bits)
{
std::stringstream ss;
ss << std::fixed << std::setprecision(bits) << number;
ss >> number;
return number;
}

3.std::stoi/stol/stoll 函数,字符串转换成整数,但不会四舍五入。

  string str1="12.76";
int i1=stoi(str1);//
qDebug()<<i1;

4.ascii值转换16进制。例如:456转换成0x456

uint8_t uavcan_centeral::parseHex(uint8_t* line, uint8_t len, uint32_t* value)
{
*value = ;
while (len--) { if (*line == )
{
return ;
}
*value <<= ;
if ((*line >= '') && (*line <= ''))
{
*value += *line - '';
}
else if ((*line >= 'A') && (*line <= 'F'))
{
*value += *line - 'A' + ;
}
else if ((*line >= 'a') && (*line <= 'f'))
{
*value += *line - 'a' + ;
}
else
{
return ;
} line++;
}
return ;
}

使用:

    uint8_t *id=new uint8_t[];
id[]=;//
id[]=;//
id[]=;//
uint32_t v;
parseHex(&id[],,&v);
qDebug()<<"v:"<<v;//v: 1110

4.int转化16进制的字符串

#include <sstream>
#include <string>
std::string MainWindow::dec2hex(int dec, int width)
{
std::stringstream ioss; //定义字符串流
std::string s_temp; //存放转化后字符
ioss << std::hex <<dec; //以十六制形式输出
ioss >> s_temp;
std::string s(width - s_temp.size(), ''); //补0
s += s_temp; //合并
return s;
}

使用:

  int dec=;
qDebug()<<dec2hex(dec,).c_str();

输出:01e240

5.数组转换16进制字符串

#include <iostream>
#include <sstream>
#include <iomanip>
#include <string>
#include <stdio.h>
std::string MainWindow::binToHexString(const unsigned char *data,size_t size)
{
std::ostringstream strHex;
strHex << std::hex << std::setfill('');
for (size_t i = ; i < size; ++i)
{
strHex << std::setw()<<std::setiosflags(std::ios::uppercase)<<static_cast<unsigned int>(data[i])<<' ';
}
return strHex.str();
}

使用

   unsigned char*data=new unsigned char[];
data[]=0x11;
data[]=0x02;
data[]=0x03;
data[]=0x04;
data[]=0x05;
data[]=0x0A;
data[]=0x0D;
unsigned long send_size= serialPort->send(data,);
ui->teMsg->append(QString::fromStdString(binToHexString(data,)));

输出:11 02 03 04 05 0A 0D

6.ascii(字符串)转换整数或16进制数。(SLCAN 串口转CAN 用到)

  std::string str="t023";
int size=str.size();
for(int i=;i<size;i++)
{
qDebug()<<QString::number(charToHex(str[i]),);
} uint8_t MainWindow::charToHex(uint8_t ch)
{ if((ch>=)&&(ch<=))
{
ch +=0x30;
}
else if((ch>=)&&(ch <=))//大写字母
{
ch +=0x37;
}
return ch;
}

7.16进制的字符串转换数值

long MainWindow::asciiToHexValue(std::string line, uint8_t len)
{
long value=;
for(int i=;i<len;i++)
{
value<<=;
char c=(char)line[i];
if ((c >= 'A') && (c <= 'Z'))
{
value+= c - 'A' + ;
}
else if ((c >= 'a') && (c <= 'z'))
{
value+= c - 'a' + ;
}
else if ((c >= '') && (c <= ''))
{
value+= c - '';
} }
return value;
}

qDebug()<<"asciiToHexValue:"<<asciiToHexValue("F7F3",4);

asciiToHexValue: 63475

8.数值转换ASCII 字符

std::string DataWidget::hexToAscii(const std::vector<uint8_t> &data)
{
std::string str="";
uint size=data.size();
for(uint i=;i<size;i++)
{
str+=(char)data[i];
}
return str;
}

9.2个8位数据high、low合成一个16位数据

2个8位数据high、low合成一个16位数据s:
s = (short) (high << ) | low;
一个16位数据s拆分成2个8位数据high、low:
high = (s >> ) & 0xff; //高8位
low = s & 0xff; //低8位

10.两个字节转换整数,高字节在前

 uint8_t data[]={0x1E,0x00};
uint16_t value=(uint16_t)(data[]<<)+data[];

11.如果是有符合(正负)的两个字节转换整数,高字节在前

  uint8_t data[]={0xff,0xff};
int16_t value=(int16_t)(data[]<<)+data[];