C++相关的简单使用

1、C++中int与string转换的方法
#include<stdio.h>
#include <stdlib.h>
string Integer2String(int i)
{
      stringstream s;
      s << i;
      return s.str();
}
int String2Integer(string str)
{
      int a;
      stringstream s;
      s << str; //将str转化为流s
      s >> a;   //将流s转化为整型a
      return a;
}
2、C++相关关键字用法

​ 参考链接:https://cplusplus.com/reference/vector/

3、C++中,char数组和string互相转换
string s = “hello world”;
char buffer[256];
memset(buffer,0,256);
char * p = s.c_str();   //string -- > char*
strncpy(buffer,s.c_str();s.length());
 char c[] = “hello world”;  //这个数组实际是以\0结束的
 string t(c);
 
char d[10] = {‘a’,’b’,’c’,’d’,’e’,’f’,’g’,’h’,’i’,’j’};  //这个数组没有结束字符串
string t2;
t2.assign(d,10);//必须使用该方法
4、C++高低字节转换方法
//float高低字节转换
bool PotC::convertReal2Double(LOG& log, const char* bufferReal, int bufferLen, double& fValue)
{
      if (bufferLen != 4)
            return false;
      // REAL值打印输出
      int nTmpVal = 0;
      memcpy(&nTmpVal, bufferReal, bufferLen);
      std::bitset<32> bits(nTmpVal);
      //log.Debug() << "....value=" << bits.to_string() << endl;
      // 按real规则,拆分符号位、指数、尾数(参考源:https://wenku.baidu.com/view/ba8e7609195f312b3069a577.html)
      int signVal = 0;
      bitset<8>   poweBits;
      bitset<23>  tailBits;
      // 符合位
      signVal = bits[31];
      // 指数
      int m = 0;
      for (int n = 23; n < 31; n ++)
      {
            poweBits[m] = bits[n];
            m ++;
      }
      // 尾数
      m = 0;
      for (int n = 0; n < 23; n ++)
      {
            tailBits[m] = bits[n];
            m ++;
      }
      char szTmp[64];
      // 输出拆分结果
      // 尾数
      sprintf(szTmp, "%23.23s%9.9s", tailBits.to_string().c_str(), " ");
      //log.Debug() << "....taile=" << szTmp << endl;
      // 指数
      sprintf(szTmp, "%23.23s%8.8s%1.1s", " ", poweBits.to_string().c_str(), " ");
      //log.Debug() << "....power=" << szTmp << endl;  
      // 符合
      sprintf(szTmp, "%31.31s%d", " ", signVal);
      //log.Debug() << "....signe=" << szTmp << endl;
      // 计算
      double fTailValue = 0;
      // 计算尾数部份
      m = tailBits.size()-1;
      for (int n = 0; n < tailBits.size(); n ++)
      {
            bool bBit = tailBits[n];
            if (!bBit)
                  continue;
            fTailValue += pow((double)2L, (double)(-1) * (tailBits.size() - n));
      }
      //log.Debug() << "尾数=" << fTailValue << endl;
      //log.Debug() << "指数=" << poweBits.to_ulong() << endl;
      // 计算总值
      fValue =(double)pow((double)-1L, (double)signVal)
            * (1+ fTailValue)
            * (double)pow((double)2L, (double)poweBits.to_ulong() - 127);
      return true;
}

//int高低字节转换
bool PotC::convertBufferEndian(char* buffer, int bufferLen)
{
      bool bResult = false;
      if (bufferLen == 4)
      {
            int nByte1 = buffer[1];
            int nByte3 = buffer[3];
            buffer[3] = buffer[0];
            buffer[0] = nByte3;
            buffer[1] = buffer[2];
            buffer[2] = nByte1;
            bResult = true;
      }
      else if (bufferLen == 2)
      {
            int nTmp = buffer[0];
            buffer[0] = buffer[1];
            buffer[1] = nTmp;
            bResult = true;
      }
      return bResult;
}

//double高低字节转换
bool PotC::convertReal2Double(LOG& log, const char* bufferReal, int bufferLen, double& fValue)
{
      if (bufferLen != 4)
            return false;
      // REAL值打印输出
      int nTmpVal = 0;
      memcpy(&nTmpVal, bufferReal, bufferLen);
      std::bitset<32> bits(nTmpVal);
      //log.Debug() << "....value=" << bits.to_string() << endl;
      // 按real规则,拆分符号位、指数、尾数(参考源:https://wenku.baidu.com/view/ba8e7609195f312b3069a577.html)
      int signVal = 0;
      bitset<8>   poweBits;
      bitset<23>  tailBits;
      // 符合位
      signVal = bits[31];
      // 指数
      int m = 0;
      for (int n = 23; n < 31; n ++)
      {
            poweBits[m] = bits[n];
            m ++;
      }
      // 尾数
      m = 0;
      for (int n = 0; n < 23; n ++)
      {
            tailBits[m] = bits[n];
            m ++;
      }
      char szTmp[64];
      // 输出拆分结果
      // 尾数
      sprintf(szTmp, "%23.23s%9.9s", tailBits.to_string().c_str(), " ");
      //log.Debug() << "....taile=" << szTmp << endl;
      // 指数
      sprintf(szTmp, "%23.23s%8.8s%1.1s", " ", poweBits.to_string().c_str(), " ");
      //log.Debug() << "....power=" << szTmp << endl;  
      // 符合
      sprintf(szTmp, "%31.31s%d", " ", signVal);
      //log.Debug() << "....signe=" << szTmp << endl;
      // 计算
      double fTailValue = 0;
      // 计算尾数部份
      m = tailBits.size()-1;
      for (int n = 0; n < tailBits.size(); n ++)
      {
            bool bBit = tailBits[n];
            if (!bBit)
                  continue;
            fTailValue += pow((double)2L, (double)(-1) * (tailBits.size() - n));
      }
      //log.Debug() << "尾数=" << fTailValue << endl;
      //log.Debug() << "指数=" << poweBits.to_ulong() << endl;
      // 计算总值
      fValue =(double)pow((double)-1L, (double)signVal)
            * (1+ fTailValue)
            * (double)pow((double)2L, (double)poweBits.to_ulong() - 127);
      return true;
}
5、C++字符串截位
std::string time = "2022-09-30 12:30:56";
std::string year = time.substr(0,4);    //从0个后面的元素开始,2022
std::string month = time.substr(5,2);    //从第5个“_”的后面元素开始,09
6、C++字符串分割
//首先需要在VS工程中配置boost的环境,若没有配置,需要按照项目需要进行考量
//引用关键的头文件
#include <boost/algorithm/string.hpp>
//包含命名空间
using namespace boost; 
int main()
{
    vector <string> vec;	
    std::string time="2022-09-30";
    boost::split(vec, time, boost::is_any_of("_"));
    //按照“_”对字符串进行分割,得到vec{2022,09,30}三个元素
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值