+ 2

Decimal to Hex in C++

I have created this program that gets a number and a base, then prints the number in the wanted value. It works well for bases of 2 to 9, but when it wants to convert a decimal number to a base bigger than 9, like 11, or 16, it prints the ASKII value of the assigned character, how can I fix it? https://code.sololearn.com/c202dhtnyFTV/?ref=app

4th Dec 2022, 6:27 PM
Root
Root - avatar
9 odpowiedzi
+ 2
The Geek , you can use this: char z[16]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'}; but this is better: char z[]="0123456789ABCDEF";
9th Dec 2022, 1:27 PM
SoloProg
SoloProg - avatar
4th Dec 2022, 9:07 PM
SoloProg
SoloProg - avatar
+ 4
You can easily convert int to hex with stringstream. https://code.sololearn.com/c1VEfwSfy8cP/?ref=app If you don't need to save the var it's even easier. https://code.sololearn.com/c807oCK050Dh/?ref=app
5th Dec 2022, 5:57 AM
Scott D
Scott D - avatar
+ 4
Someone mentioned the importance of finding your own solutions first if possible then learning to refine later to me just a few days ago (the order of comments does not show acurately): "That is the ideal way to learn.🤓 Use what you what you have, apply what you know. Refinement comes later. Making it work is the goal." Good advice I think. https://www.sololearn.com/Discuss/3109213/?ref=app Glad you were able to!
5th Dec 2022, 6:41 AM
Scott D
Scott D - avatar
+ 2
Thank you both. SoloProg and Scott D Your codes are very interesting... I found a way to fix my error, but your solutions are easier than mine. My solution : https://code.sololearn.com/cxpIC0L7S0Ob/?ref=app
5th Dec 2022, 6:34 AM
Root
Root - avatar
+ 1
Scott D thank you. :)
5th Dec 2022, 7:56 AM
Root
Root - avatar
+ 1
SoloProg Thank you so much. Why the second one is better?
20th Dec 2022, 4:57 PM
Root
Root - avatar
+ 1
The Geek , Because your code was longer so the second one works just as well but also shorter and more readable.
21st Dec 2022, 4:31 PM
SoloProg
SoloProg - avatar
+ 1
SoloProg Aha, got it, thanks.
21st Dec 2022, 5:06 PM
Root
Root - avatar