diff options
| author | Comment <tim@gray.(none)> | 2013-09-08 13:11:02 +0100 |
|---|---|---|
| committer | Comment <tim@gray.(none)> | 2013-09-08 13:11:02 +0100 |
| commit | 1ff03ea63d79549db86422837f47916a42f23eb9 (patch) | |
| tree | 1631546f60cb830749d6fe64d8af0911d9afcfe5 /rotord/src/utils.cpp | |
| parent | 512a2738e9f2fc74f109dabada357cc20c2a3412 (diff) | |
refactoring utils
Diffstat (limited to 'rotord/src/utils.cpp')
| -rw-r--r-- | rotord/src/utils.cpp | 111 |
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; +} + |
