Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2020
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.83 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4. string dec2bin(int);
  5. string dec2oct(int);
  6. string dec2hex(int);
  7. string dec2n(int, int);
  8.  
  9.  
  10. int main()
  11. {
  12. int y, p;
  13. cin>>y>>p;
  14. cout<<dec2bin(y)<<" "<<dec2oct(y)<<" "<<dec2hex(y);
  15. return 0;
  16. }
  17. string dec2bin(int x) {
  18. string w;
  19. while(x) {
  20. w=char(x%2+'0')+w;
  21. x/=2;
  22. }
  23. return w;
  24. }
  25.  
  26. string dec2oct(int x) {
  27. string w;
  28. while(x) {
  29. w=char(x%8+'0')+w;
  30. x/=8;
  31. }
  32. return w;
  33. }
  34.  
  35. string dec2hex(int x) {
  36. char t[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
  37. string w;
  38. while(x) {
  39. w=t[x%16]+w;
  40. x/=16;
  41. }
  42. return w;
  43. }
  44.  
  45. string dec2n(int x, int n) {
  46. char t[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
  47. string w;
  48. while(x) {
  49. w=t[x%16]+w;
  50. x/=n;
  51. }
  52. return w;
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement