+ 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
9 Respuestas
+ 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";
+ 4
Hope this link helps
https://code.sololearn.com/curd9c263RSp
+ 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
+ 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!
+ 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
+ 1
Scott D thank you. :)
+ 1
SoloProg
Thank you so much.
Why the second one is better?
+ 1
The Geek ,
Because your code was longer so the second one works just as well but also shorter and more readable.
+ 1
SoloProg
Aha, got it, thanks.