StrUtil.cpp
Go to the documentation of this file.
00001 /****************************************************************
00002  *
00003  * Copyright (c) 2010
00004  *
00005  * Fraunhofer Institute for Manufacturing Engineering   
00006  * and Automation (IPA)
00007  *
00008  * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00009  *
00010  * Project name: care-o-bot
00011  * ROS stack name: cob3_common
00012  * ROS package name: generic_can
00013  * Description:
00014  *                                                              
00015  * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00016  *                      
00017  * Author: Matthias Bengel, email:matthias.bengel@ipa.fhg.de
00018  * Supervised by: Christian Connette, email:christian.connette@ipa.fhg.de
00019  *
00020  * Date of creation: Feb 2009
00021  * ToDo: Check if this can be removed
00022  *
00023  * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00024  *
00025  * Redistribution and use in source and binary forms, with or without
00026  * modification, are permitted provided that the following conditions are met:
00027  *
00028  *     * Redistributions of source code must retain the above copyright
00029  *       notice, this list of conditions and the following disclaimer.
00030  *     * Redistributions in binary form must reproduce the above copyright
00031  *       notice, this list of conditions and the following disclaimer in the
00032  *       documentation and/or other materials provided with the distribution.
00033  *     * Neither the name of the Fraunhofer Institute for Manufacturing 
00034  *       Engineering and Automation (IPA) nor the names of its
00035  *       contributors may be used to endorse or promote products derived from
00036  *       this software without specific prior written permission.
00037  *
00038  * This program is free software: you can redistribute it and/or modify
00039  * it under the terms of the GNU Lesser General Public License LGPL as 
00040  * published by the Free Software Foundation, either version 3 of the 
00041  * License, or (at your option) any later version.
00042  * 
00043  * This program is distributed in the hope that it will be useful,
00044  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00045  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00046  * GNU Lesser General Public License LGPL for more details.
00047  * 
00048  * You should have received a copy of the GNU Lesser General Public 
00049  * License LGPL along with this program. 
00050  * If not, see <http://www.gnu.org/licenses/>.
00051  *
00052  ****************************************************************/
00053 
00054 #include <cob_utilities/StrUtil.h>
00055 #include <algorithm>
00056 
00057 std::string StringToUpper(std::string strToConvert) {
00058         for(unsigned int i=0;i<strToConvert.length();i++)
00059         {
00060                 strToConvert[i] = toupper(strToConvert[i]);
00061         }
00062         return strToConvert;
00063 }
00064 
00065 std::string StringToLower(std::string strToConvert) {
00066         for(unsigned int i=0;i<strToConvert.length();i++)
00067         {
00068                 strToConvert[i] = tolower(strToConvert[i]);
00069         }
00070         return strToConvert;
00071 }
00072 
00073 std::string NumToString(const int n) {
00074         std::stringstream ss;
00075         ss << n;
00076         return ss.str();
00077 }
00078 
00079 std::string NumToString(const unsigned int n) {
00080         std::stringstream ss;
00081         ss << n;
00082         return ss.str();
00083 }
00084 
00085 std::string NumToString(const long l) {
00086         std::stringstream ss;
00087         ss << l;
00088         return ss.str();
00089 }
00090 
00091 std::string NumToString(const float f, unsigned int width, unsigned int precise) {
00092         std::stringstream ss;
00093         ss << std::setw(width) << std::setprecision(precise) << f;
00094         return ss.str();
00095 }
00096 
00097 std::string NumToString(const double d, unsigned int width, unsigned int precise) {
00098         std::stringstream ss;
00099         ss << std::setw(width) << std::setprecision(precise) << d;
00100         return ss.str();
00101 }
00102 
00106 char* itoa( int value, char* result, int base ) 
00107 {
00108         // check that the base if valid
00109         if (base < 2 || base > 16) { 
00110                 *result = 0; return result; 
00111         }
00112 
00113         char* out = result;
00114         int quotient = value;
00115         
00116         do {
00117                 *out = "0123456789abcdef"[ std::abs( quotient % base ) ];
00118                 ++out;
00119                 quotient /= base;
00120         } while ( quotient );
00121 
00122         // Only apply negative sign for base 10
00123         if ( value < 0 && base == 10) *out++ = '-';
00124 
00125         std::reverse( result, out );
00126         *out = 0;
00127         return result;
00128 }
00129 
00133 std::string itoa(int value, int base) 
00134 {
00135         enum { kMaxDigits = 35 };
00136         std::string buf;
00137         
00138         buf.reserve( kMaxDigits ); // Pre-allocate enough space.
00139         
00140         // check that the base if valid
00141         if (base < 2 || base > 16) return buf;
00142         
00143         int quotient = value;
00144 
00145         // Translating number to string with base:
00146         do {
00147                 buf += "0123456789abcdef"[ std::abs( quotient % base ) ];
00148                 quotient /= base;
00149         } while ( quotient );
00150         
00151         // Append the negative sign for base 10
00152         if ( value < 0 && base == 10) buf += '-';
00153         std::reverse( buf.begin(), buf.end() );
00154         return buf;
00155 }
00156 


cob_utilities
Author(s): Christian Connette
autogenerated on Sun Oct 5 2014 23:00:37