Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Generate a number based on a given number of digits

I’m trying to write a program that generates a hex number based on a given number of digits. For instance, say that the user inputs 3, I want to generate a hex number of 0x000 and store it in some variable. Another example, say the user inputs 4 the hex number to be generated will be 0x0000.

Any ideas?

Thanks in advance.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

0x0, 0x00, 0x000 and so on, when stored in a variable, all have the same effect.
What you want to do is to store the desired number of digits in a separate variable and apply that configuration when selecting a representation of the value (be it 0 or otherwise) in output.

For doing that in C++, I recommend the formatting features provided as described e.g. here:
https://en.cppreference.com/w/cpp/io/manip/setw
and at the links you find on that page as "See also".

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading