Add leading zero's to string, without (s)printf

后端 未结 7 1828
孤独总比滥情好
孤独总比滥情好 2020-12-08 19:49

I want to add a variable of leading zero\'s to a string. I couldn\'t find anything on Google, without someone mentioning (s)printf, but I want to do this without (s)printf.<

7条回答
  •  情书的邮戳
    2020-12-08 20:07

    memcpy(target,'0',sizeof(target));
    target[sizeof(target)-1] = 0;
    

    Then stick whatever string you want zero prefixed at the end of the buffer.

    If it is an integer number, remember log_base10(number)+1 (aka ln(number)/ln(10)+1) gives you the length of the number.

提交回复
热议问题