Printf int to char array with spaces left

I have a slightly dead brain moment.

I need to keep the string view intin char[], but the ascii view should be left filled with spaces. A snprintfwill do the job.

  char data     [6];
  int msg_len = 10;
  std::snprintf(data, 6, "%*d", 5, msg_len);
  //"   10" <-- OK

I'm just wondering if there is a more elegant way to do this. I have access. C++11 There is also a small problem, I think snprintf will add a trailing character, and I should avoid this. I could have an intermediate buffer and copy it into my data, but that would add extra complexity.

I need to do this in place because these data structures are part of the message that I have to send to a server that accepts the input format in this way.

The message looks like this:

  struct
  {
    char first_field   [6];
    char second_field  [8];
    char data_field    [12];
  };

_, . , , . int , .

+4
4

:

#include <algorithm>
#include <cstddef>
#include <cassert>

inline void 
unsigned_to_decimal( unsigned long number, char* buffer, std::size_t size)
{
    std::size_t i = size;
    buffer[size - 1] = '0';
    for (unsigned long n ;(n = number) > 0 && i > 0 ;) {
        buffer[--i] = '0' + n - (10 * (number /= 10));
    }
    assert(number == 0);
    std::fill(buffer,buffer + (i - (i == size)),' ');
}

:

#include <iostream>
#include <string>
#include <climits>
#include <array>

constexpr std::size_t decimal_digits(unsigned long n)
{
    return n / 10 > 0 ? 1 + decimal_digits(n / 10) : 1;
}


int main()
{
    const std::size_t max_digits = decimal_digits(ULONG_MAX);
    std::cout << "Print decimal 0, UINT_MAX, ULONG_MAX "
        "from left-padded char buffer, size " << max_digits << ":-\n"; 
    for (auto ul : std::array<unsigned long,3>{0,UINT_MAX,ULONG_MAX}) {
        char buf[max_digits];
        std::fill(buf,buf + max_digits,'?');
        std::cout << '[' << std::string(buf,buf + max_digits) << "]\n";
        unsigned_to_decimal(ul,buf,max_digits);
        std::cout << '[' << std::string(buf,buf + max_digits) << "]\n";
    }
    return 0;
}

:

Print decimal 0, UINT_MAX, ULONG_MAX from left-padded char buffer, size 20:-
[????????????????????]
[                   0]
[????????????????????]
[          4294967295]
[????????????????????]
[18446744073709551615]

(g++ -Wall -std = ++ 11 -pedantic, GCC 5.3.1)

+1

std::ostringstream io-, : std::setfill, std::setw :

std::ostringstream s;
s << std::setfill('0') << std::setw(7) << 1015;
std::string str = s.str();
std::cout << str << std::endl; // outputs "0001015"
+5

, .

. , , .

, , snprintf , .

snprintf.

, .

, . , std::ostringstream .

, , .

0

, , , . unsigned locale, , .

inline void unsigned_to_decimal( unsigned long number, char* buffer, std::size_t size)
{
    char* start = buffer;
    char* end   = buffer + size;

    if(number == 0)
    {
        *buffer++ = '0';
    }
    else
    {
        while( number != 0 )
        {
            *buffer++ = '0' + number % 10;
            number /= 10;
        }
        std::reverse( start, buffer );

        //need to fill the remaining characters with spaces
        std::fill(buffer, end, ' ');
        std::rotate(start, start + std::distance(start, buffer), end);
    }
}
0

All Articles