十六进制到十进制转换

时间:2016-09-21 05:15:04

标签: c++ hex

我必须将十六进制转换为十进制值,然后将其乘以10,然后将其转换回来。我想出了大部分问题,当我将它乘以10并将其转换回十六进制值时,问题就会发生。

我知道有一些简单的方法可以使用char数组完成。我似乎无法弄清楚出了什么问题。

#include <iostream>
#include <cstring>
using namespace std;

char decToHex(int dec)
{
char hex;
switch (dec)
{
case 0: hex = '0';
    break;
case 1: hex = '1';
    break;
case 2: hex = '2';
    break;
case 3: hex = '3';
    break;
case 4: hex = '4';
    break;
case 5: hex = '5';
    break;
case 6: hex = '6';
    break;
case 7: hex = '7';
    break;
case 8: hex = '8';
    break;
case 9: hex = '9';
    break;  
case 10: hex = 'A';
    break;

case 11: hex = 'B';
    break;
case 12: hex = 'C';
    break;
case 13: hex = 'D';
    break;
case 14: hex = 'E';
    break;
case 15: hex = 'F';
}
return hex;
}


void intToHex(int intVal, char hexArr[])
{
int remainder;
int i = 0;

while (intVal > 0)
{
    remainder = intVal % 16;
    hexArr[i] = decToHex(remainder);
    intVal = intVal / 16;
    i++;
}
}

int hexToDec(char hex)
{
int dec;
switch (hex)
{
    case '0': dec = 0;
              break;
    case '1': dec = 1;
        break;
    case '2': dec = 2;
        break;
    case '3': dec = 3;
        break; 
    case '4': dec = 4;
        break;
    case '5': dec = 5;
            break;
    case '6': dec = 6;
        break;
    case '7': dec = 7;
        break;
    case '8': dec = 8;
        break;
    case '9': dec = 9;
        break;

    case 'A':
    case 'a': dec = 10;
        break;
    case 'B': 
    case 'b': dec = 11;
        break;
    case 'C':
    case 'c': dec = 12;
        break;
    case 'D':
    case 'd': dec = 13;
        break;
    case 'E':
    case 'e': dec = 14;
        break;
    case 'F':
    case 'f': dec = 15;
}
return dec;
}

int main()
{
char binNum[17];
char hexa[17] = { '\0' };
short age = 23;

cout << "Enter hexadecimal: ";

cin >> binNum;


cout << "You typed: " << binNum << "\n";

int count = strlen(binNum);

double powerVal = 0.0;

int decValue = 0;

for (int i = count - 1; i >= 0 ; i--)
{
    cout << hexToDec(binNum[i]) << "\n";
    decValue += hexToDec(binNum[i]) * pow(16, powerVal);
    powerVal++;

}

cout << "Decimal value is " << decValue << "\n";

intToHex(decValue*10, hexa);

cout << "Hexadecimal is: " << hexa << endl;

return 0;
}

2 个答案:

答案 0 :(得分:2)

在函数intToHex中,您将反向插入字符。试试这段代码:

#include <iostream>
#include <cstring>
#include <math.h>
#include <stack>
using namespace std;

char decToHex(int dec) {
  char hex;
  switch (dec) {
  case 0:
    hex = '0';
    break;
  case 1:
    hex = '1';
    break;
  case 2:
    hex = '2';
    break;
  case 3:
    hex = '3';
    break;
  case 4:
    hex = '4';
    break;
  case 5:
    hex = '5';
    break;
  case 6:
    hex = '6';
    break;
  case 7:
    hex = '7';
    break;
  case 8:
    hex = '8';
    break;
  case 9:
    hex = '9';
    break;
  case 10:
    hex = 'A';
    break;

  case 11:
    hex = 'B';
    break;
  case 12:
    hex = 'C';
    break;
  case 13:
    hex = 'D';
    break;
  case 14:
    hex = 'E';
    break;
  case 15:
    hex = 'F';
  }
  return hex;
}


void intToHex(int intVal, char hexArr[]) {
  int remainder;
  int i = 0;
  int j = 0;
  stack <char> s;
  while (intVal > 0) {
    remainder = intVal % 16;
    s.push(decToHex(remainder));
    intVal = intVal / 16;
  }
  while (!s.empty()) {
    hexArr[i] = s.top();
    s.pop();
    i++;
  }
}

int hexToDec(char hex) {
  int dec;
  switch (hex) {
  case '0':
    dec = 0;
    break;
  case '1':
    dec = 1;
    break;
  case '2':
    dec = 2;
    break;
  case '3':
    dec = 3;
    break;
  case '4':
    dec = 4;
    break;
  case '5':
    dec = 5;
    break;
  case '6':
    dec = 6;
    break;
  case '7':
    dec = 7;
    break;
  case '8':
    dec = 8;
    break;
  case '9':
    dec = 9;
    break;

  case 'A':
  case 'a':
    dec = 10;
    break;
  case 'B':
  case 'b':
    dec = 11;
    break;
  case 'C':
  case 'c':
    dec = 12;
    break;
  case 'D':
  case 'd':
    dec = 13;
    break;
  case 'E':
  case 'e':
    dec = 14;
    break;
  case 'F':
  case 'f':
    dec = 15;
  }
  return dec;
}

int main() {
  char binNum[17];
  char hexa[17] = { '\0' };
  short age = 23;

  cout << "Enter hexadecimal: ";

  cin >> binNum;


  cout << "You typed: " << binNum << "\n";

  int count = strlen(binNum);

  double powerVal = 0.0;

  int decValue = 0;

  for (int i = count - 1; i >= 0 ; i--) {
    cout << hexToDec(binNum[i]) << "\n";
    decValue += hexToDec(binNum[i]) * pow(16, powerVal);
    powerVal++;

  }

  cout << "Decimal value is " << decValue << "\n";

  intToHex(decValue * 10, hexa);

  cout << "Hexadecimal is: " << hexa << endl;

  return 0;
}

答案 1 :(得分:1)

如果您想创建自己的功能,请使用shift运算符。这是处理32位数字的示例。

std::string hex(int value)
{
    std::string result;
    std::string hexmap = "0123456789ABCDEF";

    int n[8];
    for (int i = 0; i < 8; i++)
    {
        n[i] = value & 0xF;
        value >>= 4;
    }
    for (int i = 7; i >= 0; i--)
        result += hexmap[n[i]];
    return result;
}

long hex(const std::string &str)
{
    std::string hexmap = "0123456789ABCDEF";
    long result = 0;
    int shift = 0;
    for (int i = str.length() - 1, n = 0; i >= 0; i--, n++)
    {
        size_t d = hexmap.find(str[i]);
        if (d == std::string::npos)
            return 0;//handle error
        result += (d << shift);
        shift += 4;
    }
    return result;
}

但是,标准C ++方法是使用std::stringstreamstd::stol。例如:

#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>

std::string hex(int i)
{
    std::stringstream ss;
    ss << std::hex << "0x" << i;
    std::string str = ss.str();
    return str;
}

long hex(const std::string &str)
{
    long result = 0;
    try {
        result = std::stol(str, 0, 16);
    }
    catch (...) {
        //handle error
    }
    return result;
}

int main()
{
    std::cout << hex(0xABCD) << "\n";
    std::cout << hex("10") << "\n";
    return 0;
}