StrUtil.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright 2017 Fraunhofer Institute for Manufacturing Engineering and Automation (IPA)
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *   http://www.apache.org/licenses/LICENSE-2.0
00009 
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016  
00017 
00018 #include <cob_relayboard/StrUtil.h>
00019 #include <algorithm>
00020 
00021 std::string StringToUpper(std::string strToConvert) {
00022         for(unsigned int i=0;i<strToConvert.length();i++)
00023         {
00024                 strToConvert[i] = toupper(strToConvert[i]);
00025         }
00026         return strToConvert;
00027 }
00028 
00029 std::string StringToLower(std::string strToConvert) {
00030         for(unsigned int i=0;i<strToConvert.length();i++)
00031         {
00032                 strToConvert[i] = tolower(strToConvert[i]);
00033         }
00034         return strToConvert;
00035 }
00036 
00037 std::string NumToString(const int n) {
00038         std::stringstream ss;
00039         ss << n;
00040         return ss.str();
00041 }
00042 
00043 std::string NumToString(const unsigned int n) {
00044         std::stringstream ss;
00045         ss << n;
00046         return ss.str();
00047 }
00048 
00049 std::string NumToString(const long l) {
00050         std::stringstream ss;
00051         ss << l;
00052         return ss.str();
00053 }
00054 
00055 std::string NumToString(const float f, unsigned int width, unsigned int precise) {
00056         std::stringstream ss;
00057         ss << std::setw(width) << std::setprecision(precise) << f;
00058         return ss.str();
00059 }
00060 
00061 std::string NumToString(const double d, unsigned int width, unsigned int precise) {
00062         std::stringstream ss;
00063         ss << std::setw(width) << std::setprecision(precise) << d;
00064         return ss.str();
00065 }
00066 
00070 char* itoa( int value, char* result, int base )
00071 {
00072         // check that the base if valid
00073         if (base < 2 || base > 16) {
00074                 *result = 0; return result;
00075         }
00076 
00077         char* out = result;
00078         int quotient = value;
00079 
00080         do {
00081                 *out = "0123456789abcdef"[ std::abs( quotient % base ) ];
00082                 ++out;
00083                 quotient /= base;
00084         } while ( quotient );
00085 
00086         // Only apply negative sign for base 10
00087         if ( value < 0 && base == 10) *out++ = '-';
00088 
00089         std::reverse( result, out );
00090         *out = 0;
00091         return result;
00092 }
00093 
00097 std::string itoa(int value, int base)
00098 {
00099         enum { kMaxDigits = 35 };
00100         std::string buf;
00101 
00102         buf.reserve( kMaxDigits ); // Pre-allocate enough space.
00103 
00104         // check that the base if valid
00105         if (base < 2 || base > 16) return buf;
00106 
00107         int quotient = value;
00108 
00109         // Translating number to string with base:
00110         do {
00111                 buf += "0123456789abcdef"[ std::abs( quotient % base ) ];
00112                 quotient /= base;
00113         } while ( quotient );
00114 
00115         // Append the negative sign for base 10
00116         if ( value < 0 && base == 10) buf += '-';
00117         std::reverse( buf.begin(), buf.end() );
00118         return buf;
00119 }
00120 


cob_relayboard
Author(s): Christian Connette
autogenerated on Sat Jun 8 2019 21:02:18