Decimal to Hex converter using Recursion (C++) -
is trying to write a program that converts the decimal number to a hexadecimal number which repeats the execution is. Can not use stuff like Manually need to know how to write functions. I tried to do this, but it does not actually work: Input is an integer input by user, and nothing in ss before this function is. Not sure if I fully understand how to convert from decimal to hex, but what is being suspended and working with it recurring Any idea? Edit: I added that statement that was suggested by Amazing, and withdrew the return in the switch, and now it works It's ... but not really. Here's the new code: This works, but the output is backwards, and it adds a zero to its end. If I convert 16 to hex in decimal, then gives me 010. If I give it a bigger number like 45 9 8 then gives me 6F 110. The correct hex value is 11F6 for 45 9 8. This is for school assignment. Can I really try to amend this? Or should I flip it after the function? It prints backwards as it is defined by you, basically it should look like this: / p> (psuedo code) If you are going back to the string, likewise, it should look like: (psuedo code)
cout & lt; & Lt; Hex & lt; & Lt; X & LT; & Lt; Endl;
string convertheck (intu, ostringstream & ss) {int x = num% 16; Switch (x) {Case 10: SS & lt; & Lt; "a"; Return ss.str (); break; Case 11: SS & lt; & Lt; "B"; Return ss.str (); break; Case 12: SS & lt; & Lt; "C"; Return ss.str (); break; Case 13: SS & lt; & Lt; 'D'; Return ss.str (); break; Case 14: SS & lt; & Lt; 'E'; Return ss.str (); break; Case 15: SS & lt; & Lt; 'F'; Return ss.str (); break; Default: SS & lt; & Lt; X; Return ss.str (); break; } Return Convert Hex (num / 16, ss); }
String Converthex (int number, ostringstream & ss) {int x = num% 16; If (num == 0) {SS & lt; & Lt; 0; Return ss.str (); } Switch (X) {Case 10: SS & lt; & Lt; "a"; break; Case 11: SS & lt; & Lt; "B"; break; Case 12: SS & lt; & Lt; "C"; break; Case 13: SS & lt; & Lt; 'D'; break; Case 14: SS & lt; & Lt; 'E'; break; Case 15: SS & lt; & Lt; 'F'; break; Default: SS & lt; & Lt; X; break; } Return Convert Hex (num / 16, ss); }
printhex (int i) {if i == 0 {// nothing changed; } PrintHex (i / 16);
String toHex (int i) {if (i == 0) {return ""; } Return to Hefex (I / 16) + ConvertTohex (i% 16); }
Comments
Post a Comment