[英]Hex to decimal conversion
I had to convert a hex to a decimal value then multiply it by 10 and then convert it back. 我必须将十六进制转换为十进制值,然后将其乘以10,然后再转换回十进制。 I figured out most of it and the problem happens when i multiply it by 10 and convert it back the hex value ends up reversed.
我弄清楚了大部分问题,当我将其乘以10并将其转换回十六进制值时,就会发生问题。
I know there is easier ways it just had to have been done using a char array. 我知道有一种更简单的方法,只需使用char数组即可完成。 I cant seem to figure out what is wrong.
我似乎无法弄清楚哪里出了问题。
#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;
}
In the function intToHex
you're inserting the characters into the array in reverse. 在
intToHex
函数中,您将字符反向插入数组。 Try this code: 试试这个代码:
#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;
}
If you want to make your own function then use the shift operator. 如果要创建自己的函数,请使用移位运算符。 This is example which will handle 32-bit numbers.
此示例将处理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;
}
However, the standard C++ method is to use std::stringstream
and std::stol
. 但是,标准的C ++方法是使用
std::stringstream
和std::stol
。 Example: 例:
#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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.