summaryrefslogtreecommitdiff
path: root/rotord/src/utils.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'rotord/src/utils.cpp')
-rw-r--r--rotord/src/utils.cpp111
1 files changed, 111 insertions, 0 deletions
diff --git a/rotord/src/utils.cpp b/rotord/src/utils.cpp
index 9828124..cd12aa4 100644
--- a/rotord/src/utils.cpp
+++ b/rotord/src/utils.cpp
@@ -27,3 +27,114 @@ bool fgreater(const float u,const float v){
if (v-u>FLOAT_THRESHOLD) return true;
else return false;
};
+
+//----------------------------------------
+template <>
+string toHex(const string& value) {
+ ostringstream out;
+ // how many bytes are in the string
+ int numBytes = value.size();
+ for(int i = 0; i < numBytes; i++) {
+ // print each byte as a 2-character wide hex value
+ out << setfill('0') << setw(2) << hex << (unsigned int) ((unsigned char)value[i]);
+ }
+ return out.str();
+}
+
+//----------------------------------------
+string toHex(const char* value) {
+ // this function is necessary if you want to print a string
+ // using a syntax like toHex("test")
+ return toHex((string) value);
+}
+
+//----------------------------------------
+int toInt(const string& intString) {
+ int x = 0;
+ istringstream cur(intString);
+ cur >> x;
+ return x;
+}
+
+//----------------------------------------
+int hexToInt(const string& intHexString) {
+ int x = 0;
+ istringstream cur(intHexString);
+ cur >> hex >> x;
+ return x;
+}
+
+//----------------------------------------
+char hexToChar(const string& charHexString) {
+ int x = 0;
+ istringstream cur(charHexString);
+ cur >> hex >> x;
+ return (char) x;
+}
+
+//----------------------------------------
+float hexToFloat(const string& floatHexString) {
+ union intFloatUnion {
+ int x;
+ float f;
+ } myUnion;
+ myUnion.x = 0;
+ istringstream cur(floatHexString);
+ cur >> hex >> myUnion.x;
+ return myUnion.f;
+}
+
+//----------------------------------------
+string hexToString(const string& stringHexString) {
+ stringstream out;
+ stringstream stream(stringHexString);
+ // a hex string has two characters per byte
+ int numBytes = stringHexString.size() / 2;
+ for(int i = 0; i < numBytes; i++) {
+ string curByte;
+ // grab two characters from the hex string
+ stream >> setw(2) >> curByte;
+ // prepare to parse the two characters
+ stringstream curByteStream(curByte);
+ int cur = 0;
+ // parse the two characters as a hex-encoded int
+ curByteStream >> hex >> cur;
+ // add the int as a char to our output stream
+ out << (char) cur;
+ }
+ return out.str();
+}
+
+//----------------------------------------
+float toFloat(const string& floatString) {
+ float x = 0;
+ istringstream cur(floatString);
+ cur >> x;
+ return x;
+}
+
+//----------------------------------------
+bool toBool(const string& boolString) {
+ static const string trueString = "true";
+ static const string falseString = "false";
+ string lower = Poco::toLower(boolString);
+ if(lower == trueString) {
+ return true;
+ }
+ if(lower == falseString) {
+ return false;
+ }
+ bool x = false;
+ istringstream cur(lower);
+ cur >> x;
+ return x;
+}
+
+//----------------------------------------
+char toChar(const string& charString) {
+ char x = '\0';
+ istringstream cur(charString);
+ cur >> x;
+ return x;
+}
+