How to print the leading zeroes?

I have to thank you all for the contributions and for the variety of idea and kind of approach to my question.
Until today I haven't had time to try your hints, but during all this time I have "looped" in my mind the simple core of the question that I can resume telling: <<please print always four digit, even when they start with a zero o more than one>>.

So if we can imagine two adjacent bytes of memory on the bus of a ideal basic CPU, the operation to do in assembler are indeed easy and simple. The C language cannot do exactly the same but I cannot surrender to the idea to write so much for do so few. I always think <<If I put four zero characters into four bytes of memory and then I overwrite only the least significant characters, I finally arrive to my target.>>
This don't run well in C, because writing my value of 16 bits represented in HEX, running the sketch, if the hexadecimal value begins with zero, the value became as 3 or 2 or 1 length, and the four zeroes that I've prepared go in smoke.
But I'm lucky, I can do the opposite thing: I can add (concatenate) all the zeroes I desire after the value as also before it.
Now I show you how.

[........]
GoodHex = String(RegInp, HEX); // I put my word into a String (local variable)
GoodHex.toUpperCase(); // I don't know why the String use only LowerCase
while(GoodHex.length() < 4) // I need a four digit representation
{
GoodHex="0"+GoodHex; // Put all the blessed zeroes needed
}

Serial.println(GoodHex); // Shows the coveted result

[........]

I suppose that this solution could be more heavy than other as speed and memory usage, maybe less efficient.
In my specific case this isn't matter, because this part run only when a new 16bit value it's read from 16 push-buttons. This sketch only help the operator to read in "readable" value the position of all the 16 toggle switch. It's more easy for him to tell "A5A5" than "up, down, up, down, down, up, down, up, up, down, up, down, down, up, down, up", it's true?

My project will be completed when the data that Arduino receive, will correspond ALWAYS to what another Arduino transmit and vice-versa. At that point no one operator will stay to the terminal to read the value of the 16 traveling bits and this little segment of the sketch will be deleted.

What I surely won't delete it's my solution, your help and I hope further comments.

Wow, using String to do the job... That IS really overdoing it!

umurri:
The C language cannot do exactly the same but I cannot surrender to the idea to write so much for do so few.

And

umurri:
I suppose that this solution could be more heavy than other as speed and memory usage, maybe less efficient.
In my specific case this isn't matter, because this part run only when a new 16bit value it's read from 16 push-buttons.

Don't match. By using String and even more, by concatenating Strings you do WAYYYYYYYYYYYYYYY more then whatever we showed you. A thing newbies have problems with is the amount of code they see. We posted solutions where you can see all the code and then it might look as much. You post a solution that relies on a complex library. Out of sight, of of mind is the trend among newbies. But the amount of code involved by doing that is WAYYY more. And the memory penalty wayyyy bigger. Remember, you program a micro, not a Intel Sky Lake with 64GB of memory :wink:

Although our code looks long, once written you can just call the function and don't think about it. You can even put it into a library and don't ever see it again. But what we showed you is by NO means (not even close) more complex then using String to do the dirty work so you don't have to see it :wink:

And it's not that hard to change my code to hex.

void printHexWithLeading(Print &out, unsigned long n, byte size){
  for(byte i = size - 1; ;i--){
    out.print((n >> (i * 4)) & 0x0F, HEX);
   
    if(i == 0){
      break;
    }
  }
}

inline void printHexWithLeading(Print &out, unsigned long n){
  printHexWithLeading(out, n, sizeof(n) * 2);
}

inline void printHexWithLeading(Print &out, unsigned int n){
  printHexWithLeading(out, n, sizeof(n) * 2);
}

inline void printHexWithLeading(Print &out, byte n){
  printHexWithLeading(out, n, sizeof(n) * 2);
}

I understood.
As I expected what I wrote using the Strings is underperforming, greedy flash memory and probably RAM.
Reading my C code, I imagined that apparently it seems rather short and concise, but it is only formally, while if it parses the assembler code produced would see just the opposite.
I'm not surprised. But I confess, I still happy having found a working solution (but not so good) also alone, and although I am a newbie.

I think that my solution is like to hunt the fly with a gun!

As I wrote, in this specific case does not matter to me efficiency, memory consumption, and the amplitude of the wasted code, but in general, looking just a bit further, I do mostly learn and this discussion your comments, your suggestions will surely help me in that.

I must admit that at first sight I struggle to understand how it behaves this function, although I already know what result will give me.
Well, I have to put a little patience and study .... even try.