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中的值转换成除 ...
随机推荐
- rpmbuild打包php
安装php依赖库 mkdir -pv ~/rpmbuild/{BUILD,RPMS,SOURCES,SPECS,SRPMS} php有一个依赖库,在yum源于epel源中都没有需要自己打包libico ...
- HTML第四耍 超链接标签
1.HTML 超链接(链接) 一.HTML 超链接 HTML中使用超级链接与网络上的另一个文档相连.几乎可以在所有的网页中找到链接.点击链接可以从一张页面跳转到另一张页面. 超链接可以是一个字,一个词 ...
- C#中foreach命令的使用
在Python中,for循环不仅可以用来做指定次数的循环,还可以利用for i in xxx:来实现元素的遍历,遍历的对象几乎可以是任意格式.而在C++以及C#中,除了普通的for循环之外,也提供了这 ...
- [Java] [查找文件] [递归]]
// 工具方法 private static FilenameFilter getFilter(final String mode) { return new FilenameFilter() { P ...
- Sqlserver自动优化
(1)select a.* from tb1 a left join tb2 b on a.id=b.id where a.name='1' (2)select * from (select a. ...
- Newtonsoft.Json 的基本用法
Ø 前言 说起 C# 对 JSON 的操作(序列化与反序列化),大家都会想到 JavaScriptSerializer.DataContractJsonSerializer 与 Newtonsoft ...
- HIbernate处理数据更新丢失
使用乐观锁的机制处理: 第一步: 在持久类中添加version属性,并且添加对应的get.set方法; 第二步: 在全局配置文件中配置节点<version name="version& ...
- Burpsuite之Burp Collaborator模块介绍
Burp Collaborator.是从Burp suite v1.6.15版本添加的新功能,它几乎是一种全新的渗透测试方法.Burp Collaborator.会渐渐支持blind XSS,SSRF ...
- luogu 2051 中国象棋
非常好的dp,锻炼思维 f[i][j][k] 前i行有j列放1,k列放2 #include<bits/stdc++.h> #define int long long #define rep ...
- SQL Server 远程备份详解
例1: 有A数据库服务器,B本机: 我现在想通过在B机器上通过代码调用SQL来执行A数据库的备份到B机器上 调用的SQL语句为:Backup Database MYDATABASE To Disk=' ...