padding with sprintf

前端 未结 7 2050
甜味超标
甜味超标 2020-12-17 07:34

I have a dummy question. I would like to print an integer into a buffer padding with 0 but I cannot sort it out the sprintfformat. I am trying the following

7条回答
  •  清酒与你
    2020-12-17 08:04

    A fairly effective version that doesn't need any slow library calls:

    #include 
    
    void uint_tostr (unsigned int n, size_t buf_size, char dst[buf_size])
    {
      const size_t str_size = buf_size-1;
    
      for(size_t i=0; i

    This can be optimized further, though it is still probably some hundred times faster than sprintf as is.

提交回复
热议问题