.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);
}