Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8242477
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T21:10:59+00:00 2026-06-07T21:10:59+00:00

Problem -> To return fixed length string to std::string*. Target machine -> Fedora 11

  • 0

Problem -> To return fixed length string to std::string*.
Target machine -> Fedora 11 .

I have to derive a function which accepts integer value and return fixed lenght string to a string pointer;
for example -> int value are in range of 0 to -127

so for int value 0 -> it shoud display 000
for value -9 -> it should return -009
for value say -50 -> it should return -050
for value say -110 -> it should return -110

so in short , lenght should be same in all cases.

What I have done : I have defined the function according to the requirement which has shown below.

Where I need help: I have derived a function but I am not sure if this is correct approach. When I test it on standalone system on windows side , the exe stopped working after sometimes but when I include this function with the overall project on Linux machine , it works flawlessly.

    /* 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;

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T21:11:01+00:00Added an answer on June 7, 2026 at 9:11 pm

    I have nothing against the versions that use streams, but you can do it all yourself more simply than your code:

    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;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an odd problem when using the return result of a function and
I have problem understanding what the return fibonacci( number-1 ) + fibonacci( number-2 )
I have a problem with Json formating returned from .ASMX I need to return
I have a problem when using jsonp to API HTTPS website. it will return
$(window).bind(beforeunload,function(){ return cant be seen in ff; }); The problem is that i can't
I have a number of rather large binary files (fixed length records, the layout
I took over another project and have fixed everything else except this one problem.
I have a helper function that takes an unsigned char array of a fixed
We got a problem with NUnit 2.5.3: nunit-console.exe does not return after finishing all
While developing an application, I had the following problem. I wanted to return an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.