C ++中的十进制到十六进制转换代码

时间:2015-04-20 22:16:00

标签: c++

我是C ++编程的全新手。我已经被赋予了将十进制转换为十六进制转换的任务。这就是我到目前为止所做的。

#include <iostream>

int main()
{
    long dec;
    int rem;

    std::cout << "enter decimal number: ";
    std::cin >> dec;

    while (dec > 0) {
        rem = dec % 16;
        if (rem > 9) {
            switch (rem) {
               case 10: std::cout << "A"; break;
               case 11: std::cout << "B"; break;
               case 12: std::cout << "C"; break;
               case 13: std::cout << "D"; break;
               case 14: std::cout << "E"; break;
               case 15: std::cout << "F"; break;
            }
        }
        else {
            std::cout << rem;
        }
        dec = dec / 16;
    }
}

它向后回答,就像我输入一个十进制数'650',当答案应该是A82时,它给出十六进制值28A。任何人都可以建议我应该做些什么来纠正它?

8 个答案:

答案 0 :(得分:8)

最简单的解决方案:

#include <iostream>
using namespace std;
int main(){
    int val;
    cin >> val;
    cout << hex << val << endl;
    return 0;
}

算法的修复 - 使用递归:

#include <iostream>
#include <conio.h>
using namespace std;

void print_hex(int value)
{
    if (value == 0)
        return;

    int rem = value % 16;
    value /= 16;
    print_hex(value); //first execute recurency and print next value

    //after return print the less significant digit
    if (rem > 9)
        cout << (char)(rem - 10 + 'A');
    else
        cout << rem;
}

int main(int argc, char * argv[]) {
    cout << "enter decimal number: ";
    long dec;
    cin >> dec;
    print_hex(dec);
    cout << endl;
    return 0;
}

答案 1 :(得分:5)

当然它会给出答案。您的程序开始分析给定的十进制数“从后面开始”。具体而言,您要转换以最不重要的数字开头的数字。处理完每个数字后,立即输出。因此,您首先输出最不重要的数字,但十六进制和十进制系统都表示从最重要的数字开始的数字。换句话说,如果您的数字是十六进制的XYZ(以十进制表示16^2 * X + 16^1 * Y + 16^0 * Z),则将其输出为ZYX。尝试保存字符串中的所有数字,在其开头添加新数字:

string s = "";
while (dec > 0)   // Do this whilst the quotient is greater than 0.
{
  rem = dec % 16; // Get the remainder.
  if (rem > 9)
  {
    // Map the character given that the remainder is greater than 9.
    switch (rem)
    {
      case 10: s = "A" + s; break;
      case 11: s = "B" + s; break;
      case 12: s = "C" + s; break;
      case 13: s = "D" + s; break;
      case 14: s = "E" + s; break;
      case 15: s = "F" + s; break;
    }
  }
  else
  {
    s = char(rem + 48) + s; // Converts integer (0-9) to ASCII code.
    // x + 48 is the ASCII code for x digit (if 0 <= x <= 9)
  }
  dec = dec/16;
 }
 if (s == "") // if the number was 0, the string will remain empty
   cout << "0";
 else
   cout << s;

答案 2 :(得分:2)

如果唯一错误的是输出的顺序与您想要的顺序相反,则可以将输出放入容器中。然后,在打印出来之前,反转容器中输出的顺序。

将输出转换为容器的一种方法是将输出发送到stringstream

std::ostringstream sout;
const char *hex = "0123456789ABCDEF";
while (dec > 0) {
    rem = dec % 16;
    sout << hex[rem];
    dec = dec / 16;
}
std::string s(sout.str());
std::reverse(s.begin(), s.end());
std::cout << s;

答案 3 :(得分:1)

最简单的方法:将其放入字符串中:

string answer = "";
while (dec > 0) {
    rem = dec % 16;
    if (rem > 9) {
        switch (rem) {
           case 10: answer = "A" + answer; break;
           case 11: answer = "B" + answer; break;
           case 12: answer = "C" + answer; break;
           case 13: answer = "D" + answer; break;
           case 14: answer = "E" + answer; break;
           case 15: answer = "F" + answer; break;
        }
    }
    else {
        answer = string(1, (char)rem-'0') + answer;
    }
    dec = dec / 16;
}
cout << answer;

答案 4 :(得分:1)

如果要将十进制转换为二进制(基数2),十六进制(基数16)等解决方案,请执行此解决方案!

#include <iostream>
using namespace std;

void d2base(int n, int b);
char arr[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};

int main()
{
    int num, base;

    cout << "Enter decimal number to convert: ";
    cin >> num;
    cout << "Enter the base: ";
    cin >> base;
    cout << "\nNumber in " << base << " is ";

    d2base (num, base);
}

void d2base(int n, int b)
{
    if (n > 0)
    {
        d2base(n/b, b);
        cout << arr[n%b];
    }
}

答案 5 :(得分:1)

我现在正在使用MFC Cpp,并使用CString:

CString hex;
hex.Format("%x", 16766208); //ffd500 - use capital %X for FFD500

答案 6 :(得分:1)

我的解决方案:

/*  decimal /hexa
    Jose Anilto
*/
#include <iostream>
#include <cstring>


using namespace std;


string converte(int n, string x) {

    string hexa = {"0123456789ABCDEF"};
    return (n < 16 ? "0x" + (hexa[n] + x) : converte(n/16, hexa[n%16] + x));

}

int main () {

    int nd ;
    string y = "";
    cout << "\n Digite um numero: "; cin >> nd;
    cout << "\n Numero digitado: " << nd << "\n";

    cout << "\n Hexadecimal: " << converte(nd, y) << "\n";

 return 0;
}

答案 7 :(得分:1)

将十进制转换为十六进制。

无需求助于递归,就足以在最大可能的本地缓冲区中形成答案并向后填充答案。

对于代码转储,此任务太有意义了。而是提供一种算法来指导。

  // Pseudo code
  long x
  L = MaxSizeNeeded(long) // hint:sizeof
  buffer[L]
  pointer p = last_element(buffer)
  *p = 0 // null character
  do {
    extract least base 16 digit of `x`
    *--p = text version of least digit
    x /= base
  } while (x)
  prepend "0x" if desired.
  Form string from `p` and return

x < 0时还涉及其他问题。将代码移至unsigned long或从输入-27形成“ -0x1B”,将值折叠到负数,如上所述转换,知道最小数字将为负数,最后根据需要应用符号。