从整数值返回固定长度的std :: string

sam*_*tha 3 c++ string stringstream

问题 - >将固定长度字符串返回到std :: string*.
目标机器 - > Fedora 11.

我必须派生一个函数,它接受整数值并将固定的长度字符串返回给字符串指针 ;
例如 - > int value的范围是0到-127

所以对于int值0 - >它显示000
为值-9 - >它应该返回-009
表示值-50 - >它应该返回-050
表示值为-110 - >它应该返回-110

所以简而言之,在所有情况下,长度应该相同.

我做了什么:我已经根据下面的要求定义了这个功能.

我需要帮助的地方:我已经派生了一个函数,但我不确定这是否是正确的方法.当我在Windows端的独立系统上测试它时,exe有时会停止工作,但是当我在Linux机器上包含这个功能和整个项目时,它可以完美地工作.

    /* function(s)to implement fixed Length Rssi */
 std::string convertString( const int numberRssi, std::string addedPrecison="" )
 {
     const std::string         delimiter   =   "-";
                stringstream   ss;

     ss  << numberRssi ;
     std::string tempString = ss.str();
     std::string::size_type found = tempString.find( delimiter );
     if( found == std::string::npos )// not found
     {
         tempString = "000";
     }
     else
     {
         tempString = tempString.substr( found+1 );
         tempString = "-" +addedPrecison+tempString ;
     }
     return  tempString;

 }

 std::string stringFixedLenght( const int number )
 {
     std::string str;
     if( (number <= 0) && (number >= -9) )
       {
           str = convertString( number, "00");
       }
       else if( (number <= -10) && (number >= -99) )
       {
           str = convertString( number, "0");
       }
       else
       {
           str= convertString(number, "");
       }
     return str;
 }
// somewhere in the project calling the function
ErrorCode A::GetNowString( std::string macAddress, std::string *pString )
{
    ErrorCode result = ok;
    int lvalue;
    //some more code like iopening file and reading file 
    //..bla
    // ..bla     
    // already got the value in lvalue ;

    if( result == ok )
    {
         *pString = stringFixedLenght( lValue );
    }

    // some more code

    return result;

}
Run Code Online (Sandbox Code Playgroud)

das*_*ght 13

您可以使用I/O操纵器设置所需的宽度,并填充零.例如,该程序打印00123:

#include <iostream>
#include <iomanip>

using namespace std;

int main() {
    cout << setfill('0') << setw(5) << 123 << endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

你必须自己照顾负面价值观:cout << setfill('0') << setw(5) << -123 << endl印刷品0-123,而不是-0123.检查值是否为负,将宽度设置为N-1,并在前面添加减号.

  • 此解决方案不返回 `std::string`!但是将 `std::cout` 替换为 `std::stringstream` 将得到所需的解决方案。 (2认同)

Som*_*ude 10

如何使用std::ostringstream和标准输出格式操纵器?

std::string makeFixedLength(const int i, const int length)
{
    std::ostringstream ostr;

    if (i < 0)
        ostr << '-';

    ostr << std::setfill('0') << std::setw(length) << (i < 0 ? -i : i);

    return ostr.str();
}
Run Code Online (Sandbox Code Playgroud)

  • 我不得不`#include <iomanip>`来使setfill等工作. (2认同)

Ste*_*sop 0

我并不反对使用流的版本,但是您可以自己完成这一切,这比您的代码更简单:

std::string fixedLength(int value, int digits = 3) {
    unsigned int uvalue = value;
    if (value < 0) {
        uvalue = -uvalue;
    }
    std::string result;
    while (digits-- > 0) {
        result += ('0' + uvalue % 10);
        uvalue /= 10;
    }
    if (value < 0) {
        result += '-';
    }
    std::reverse(result.begin(), result.end());
    return result;
}
Run Code Online (Sandbox Code Playgroud)