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[];
c++ 值转换的更多相关文章
- ios 把毫秒值转换成日期 NSDate
ios 把毫秒值转换成日期 (比较好用) 1343359790000 这是毫秒值------最佳解决方案-------------------- long long time=134335979000 ...
- Web API-如何将Controller的返回值转换成HTTP response消息
https://www.asp.net/web-api/overview/formats-and-model-binding/json-and-xml-serialization https://co ...
- 分享:根据svg节点对象类型和路径值转换坐标值
功能用处: 对svg文件的路径节点填充时会使用(相邻两个坐标区域内的四边形的填充颜色不重复). 需要对svg文件中的Path节点或者 Polyline 节点做颜色填充.并且相邻的两个区域之间的颜色不允 ...
- sql 在将 nvarchar 值 转换成数据类型 int 时失败。
假设有存储过程:proc_test2 create proc proc_test2 @Id int begin as declare @sql varchar(max) @sql = 'select ...
- json字符串转换成json对象,json对象转换成字符串,值转换成字符串,字符串转成值
一.json相关概念 json,全称为javascript object notation,是一种轻量级的数据交互格式.采用完全独立于语言的文本格式,是一种理想的数据交换格式. 同时,json是jav ...
- python文本 字符与字符值转换
python文本 字符与字符值转换 场景: 将字符转换成ascii或者unicode编码 在转换过程中,注意使用ord和chr方法 >>> print(ord('a')) 97 ...
- Jquery把获取到的input值转换成json
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- [翻译] ColourClock 将时间值转换成背景色
ColourClock 将时间值转换成背景色 https://github.com/bennyguitar/ColourClock This project converts Time to Hex/ ...
- WPF值转换实例
WPF绑定功能非常方便,有时候点击某值时在另t一处显示此值的另一表现形式或调用其对应的其它值,用WPF值转换功能会很方便,下面就一LISTBOX和TEXTBLOCK控件,把LISTBOX中的值转换成除 ...
随机推荐
- Linux下C语言连接MySQL
本文出自 http://blog.csdn.net/shuangde800 首先保证安装: 1:安装MySQL:sudo apt-get install mysql-server mysql-cl ...
- Ubuntu vim下 实现函数跳转功能
安装sudo apt-get install exuberant-ctags 在每次使用时,需要初始化tags,只有这样才能使用跳转功能 初始化: 进入项目的顶级目录.输入以下命令. ...
- hdu 2815 Mod Tree (exBSGS)
http://acm.hdu.edu.cn/showproblem.php?pid=2815 //解 K^D ≡ N mod P #include<map> #include<cma ...
- GeoGlobe Server运维
本篇博文简单记录鄙人在管理和维护GeoGlobe Server中,遇到的一些问题以及可行的解决方案 1 关于启动内存 Server默认的启动内存是256M,当服务比较多的时候,启动就会很慢.我们可以修 ...
- Silverlight设计器——Path
如下图,在设计一个InfoWindow的时候,顶栏的关闭按钮没有出现.观察了半天,也没有弄明白.无意中,拖动一个几乎透明的信息框,突然就出现了关闭的按钮.原来,那个信息框只是一个Path,它遮住了关闭 ...
- docker 系列 - 企业级私有镜像仓库Harbor部署(转载)
本文转载自 搜云库 的文章 https://www.jianshu.com/p/7d76850de03f , 感谢作者 3.png 上一篇文章搭建了一个具有基础功能,权限认证.TLS 的私有仓库, ...
- NPOI导出Excel2007-xlsx
今天在用npoi导出xls时会报错,经过在网上查找资料,找到一篇博客文章介绍的,原文地址https://www.cnblogs.com/spring_wang/p/3160020.html 1.今天再 ...
- 1、PHP入门二维数组与循环
<?php $two=array(array(2,3),1=>array(1,2,3),2=>array(4,5,6)); echo $two[1][0];//输出1 echo $t ...
- VS中ipch文件夹和sdf文件的处理方式
ipch文件夹和sdf是VS产生的预编译头文件和智能提示信息,对编码没有影响,可存放在固定的位置,定期进行清理
- linux 截图工具 shutter
ubuntu 安装shutter sudo apt install shutter libgoo-canvas-perl libgoo-canvas-perl是提供对截图编辑功能,例如,添加画框,文字 ...