I have an integer x that contains a 1-4 digit number. How can I convert it to a 4-character array of the digits (padded with zeroes if necessary)? That is, if x is 4, I want character array y to contain 0004
// Assume x is in the correct range (0 <= x <= 9999)
char target[5];
sprintf(target, "%04d", x);
                        Well, if you are guaranteed to have only 4 elements in the vector, I think you will be able to do with with the following:
  char result[4];
  for(int i=0;i<4;++i)
  {
    result[3-i] = (value % 10);
    value /= 10; 
  }
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With