Skip to content

Instantly share code, notes, and snippets.

@masazdream
Created February 15, 2014 09:00
Show Gist options
  • Select an option

  • Save masazdream/9016448 to your computer and use it in GitHub Desktop.

Select an option

Save masazdream/9016448 to your computer and use it in GitHub Desktop.
cppでよく使う処理をまとめた。
/*
* Utils.cpp
*
* Created on: 2013/06/19
* Author: root
*/
#include "Utils.h"
using namespace std;
string int_too_string(int number) {
stringstream ss;
ss << number;
return ss.str();
}
/* stringを分割する */
vector<string> splitstring(const string& str, char delim) {
istringstream iss(str);
string tmp;
vector<string> res;
while (getline(iss, tmp, delim))
res.push_back(tmp);
return res;
}
/* string を intに変換する*/
int string_to_int(const string &str) {
int n = atoi(str.c_str());
return n;
}
/*stringを置換する*/
string Replace(string originalstr, string searchword, string destword) {
string::size_type Pos(originalstr.find(searchword));
while (Pos != string::npos) {
originalstr.replace(Pos, searchword.length(), destword);
Pos = originalstr.find(searchword, Pos + destword.length());
}
return originalstr;
}
template<typename T>
T stringTobinary(const string& text, int base) {
assert(base == 8 || base == 10 || base == 16);
std::istringstream is(text);
T value;
switch (base) {
case 8:
is >> std::oct >> value;
break;
case 10:
is >> value;
break;
case 16:
is >> std::hex >> value;
break;
default:
break;
}
return value;
}
/*stringをucharに分割する*/
vector<unsigned char> splitstring_to_uchar(const string &str, char delim) {
istringstream iss(str);
string tmp;
vector<unsigned char> res;
while (getline(iss, tmp, delim)) {
const string tmp_trim = Replace(tmp, " ", "");
int length = tmp_trim.length();
const char *cchar = tmp_trim.c_str();
//int b = (unsigned char) c;
unsigned char *uchar = new unsigned char[length + 1];
for (int i = 0; i <= length; i++) {
uchar[i] = (unsigned char) cchar[i];
}
res.push_back(*uchar);
//res.push_back(value);
}
return res;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment