C++ 数值转换

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

void MainWindow::on_pushButton_clicked()
{
   double number=3.141592;
   double result=MyRound(number,3);
   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);//3.142
   std::string str=std::to_string(number);//3.142000
   str=str.substr(0,str.find('.')+4);//"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);//12
  qDebug()<<i1;


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

uint8_t uavcan_centeral::parseHex(uint8_t* line, uint8_t len, uint32_t* value)
{
    *value = 0;
    while (len--) {

        if (*line == 0)
        {
            return 0;
        }
        *value <<= 4;
        if ((*line >= '0') && (*line <= '9'))
        {
           *value += *line - '0';
        }
        else if ((*line >= 'A') && (*line <= 'F'))
        {
           *value += *line - 'A' + 10;
        }
        else if ((*line >= 'a') && (*line <= 'f'))
        {
           *value += *line - 'a' + 10;
        }
        else
        {
           return 0;
        }

        line++;
    }
    return 1;
}

使用:

    uint8_t *id=new uint8_t[3];
    id[0]=52;//4
    id[1]=53;//5
    id[2]=54;//6
    uint32_t v;
    parseHex(&id[0],3,&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'); //补0
    s += s_temp; //合并
    return s;
}

使用:

  int dec=123456;
  qDebug()<<dec2hex(dec,6).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('0');
    for (size_t i = 0; i < size; ++i)
    {
        strHex << std::setw(2)<<std::setiosflags(std::ios::uppercase)<<static_cast<unsigned int>(data[i])<<' ';
    }
    return strHex.str();
}

 使用

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

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

 

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

 

  std::string str="t023";
  int size=str.size();
  for(int i=0;i<size;i++)
  {
     qDebug()<<QString::number(charToHex(str[i]),16);
  }


uint8_t  MainWindow::charToHex(uint8_t ch)
{

    if((ch>=0)&&(ch<=9))
    {
         ch +=0x30;
    }
    else if((ch>=10)&&(ch <=15))//大写字母
    {
       ch +=0x37;
    }
    return ch;
}

7.16进制的字符串转换数值

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

    }
    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=0;i<size;i++)
     {
        str+=(char)data[i];
     }
     return  str;
}

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

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

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

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

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

  uint8_t data[2]={0xff,0xff};   
  int16_t value=(int16_t)(data[0]<<8)+data[1];
原文地址:https://www.cnblogs.com/ike_li/p/8340725.html