c++ 常用数据处理函数

.h 文件

/*
 * commonfunction.h
 *
 *  Created on: Sep 2, 2019
 *      Author: huanglei
 */

#ifndef COMMONFUNCTION_H
#define COMMONFUNCTION_H
using namespace std;
long long String_Binary_to_Int(string String_Binary);
string Int_to_String(int n);
int charToInt(char c);
char IntTochar(int c);
string ucharTostring(unsigned char uc);
string doubleTostring(double do_b);
string hexTostring(unsigned long long hex_);
double str2double(string str);

void splitStr(const string& origStr, const char sepCh, vector<string>& array, const bool bSupportBracket = false);
double HoldDecimalPlaces(double _double, int number);
int str2Int(string str);

.cpp 文件


long long stringBinaryToInt(string stringBinary)//二进制字符串转10进制
{
    long long data = 0;
    for(int i = 0;i < int(String_Binary.size());i++)
    {
        data <<= 1;
        int tmp = charToInt(String_Binary[i]);
        data = data | tmp;

    }
    return data;
}

string intToString(int n)   //int 转string
{
    ostringstream stream;
    stream << n;
//    cout<<stream.str()<<endl;
    return stream.str();
}


int charToInt(char c)   //char 转 int
{
    if(c >= '0' && c <= '9') return c - 48;
    else if(c >= 'a' && c <= 'f') return c - 87;
    else if(c >= 'A' && c <= 'F') return c - 55;
    else
        return 0;
}

char IntTochar(int c)   //int 转char
{
    if(c >= 0 && c <= 9) return c + 48;
    else if(c >= 10 && c <= 15) return c + 55;
    //else if(c>='A'&&c<='F')  return c-60;
    else
        return '0';

}

int string2Int(string str)  //string 转 int
{
    istringstream mystream(str);
    int number;
    mystream >> number;
    return (number);
}

//字符串分割
void splitStr(const string& origStr, const char sepCh, vector<string>& array, const bool bSupportBracket)   
{
    bool bIsWithinBracket;
    string word;
    const char* str = origStr.c_str();
    array.clear();
    for(INT i = 0;*(str + i) != 0;++i)
    {
        char ch = str[i];
        /* seperator CHAR '\n' */
        if(sepCh == '\n')
        {
            if(ch == sepCh)
            {
                array.push_back(word);
                word = "";
            }
            else
            {
                word += ch;
            }
            continue;
        }

        /* for other seperator char, like ',', '"',' ", etc.*/
        if(ch == sepCh)
        {
            if(bIsWithinBracket && bSupportBracket)
            {
                word += ch;
            }
            else
            {
                array.push_back(word);
                word = "";
            }
        }
        else if(ch == '(')
        {
            bIsWithinBracket = TRUE;
        }
        else if(ch == ')')
        {
            bIsWithinBracket = TRUE;
        }
        else if(ch == ' ')
        {
            if(i == 4)
            {
                /* this space is after the FW command name, eg. "DFPN " */
                array.push_back(word);
                word = "";
            }
        }
        else
        {
            word += ch;
        }
    }

    /* origStr is like "abc,def", we pick the rest one */
    if(word.size())
    {
        array.push_back(word);
    }
}

/*
保留小数点后几位
_double:输入数
number:小数点后几位
*/
double HoldDecimalPlaces(double _double, int number)
{
    double temp = _double, k = 1.0;
    int tempInt_;
    for(int i = 0;i < number;i++)
    {
        k *= 10;
        temp = temp * 10;
    }
    tempInt_ = (int) temp;

    return (tempInt_ / k);
}
//无符号char 转 string
string ucharToString(unsigned char uc)
{
    string str = "";
    str += Int_to_String(uc / 100);
    str += Int_to_String(uc % 100 / 10);
    str += Int_to_String(uc % 10);
    return str;
}
//double 转 string
string doubleToString(double do_b)
{
    string str1;
    stringstream ss;
    ss << do_b;
    ss >> str1;
    return str1;
}
//unsigned long long转 string
string hexToString(unsigned long long hex_)
{
    stringstream ss;
    ss << hex << hex_;
    string str1;
    ss >> str1;

    return str1;
}

//string转 double
DOUBLE stringToDouble(string str)
{
    DOUBLE temp = 99999;
    if(str.find('.') == true)
    {
        vector<string> stringVec;
        stringVec.clear();
        splitStr(str, '.', stringVec);
        if(stringVec.size() == 2)
        {
            int tmp = (-1) * stringVec[1].size();
            temp = str2Int(stringVec[0]) + str2Int(stringVec[1]) * pow(10.0, tmp);
        }
        else
        {
            cerr << "str include muti point signal" << endl;
        }
    }
    else
    {
        temp = str2Int(str);
    }
    return (temp);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值