求助 十六进制和十进制的转换
我做得一个题目要求能够自由转换十进制和十六进制,现在十进制到十六进制的转换没有问题,但是十六进制到十进制的转换不成功,有哪位大大能帮我看一下,究竟要在哪儿改正,问题在哪儿?
HexaInt.h
#ifndef HEXAINT_H_
#define HEXAINT_H_
#include <iostream>
using namespace std;
class HexaInt{
private:
unsigned int decimal;
char* hex;
public:
HexaInt(int decimal=0);
HexaInt(const HexaInt& copy);
~HexaInt();
void setDecimal(int decimal);
int getDecimal()const;
void decToHex();
void hexToInt();
HexaInt operator+(const HexaInt& other)const;
HexaInt operator-(const HexaInt& other)const;
HexaInt operator*(const HexaInt& other)const;
HexaInt operator/(const HexaInt& other)const;
HexaInt operator%(const HexaInt& other)const;
HexaInt& operator=(const HexaInt& other);
char& operator[](int i);
friend ostream& operator<< (ostream& out, const HexaInt& h);
friend istream& operator>> (istream& in, HexaInt& h);
};
#endif /*HEXAINT_H_*/
HexaInt.cpp
#include "HexaInt.h"
HexaInt::HexaInt(int decimal){
int zahl = sizeof(unsigned int)*2;
this->hex = new char[zahl];
this->decimal = decimal;
decToHex();
}
HexaInt::~HexaInt(){
delete[] this->hex;
}
HexaInt::HexaInt(const HexaInt& other){
this->decimal = other.decimal;
int zahl = sizeof(unsigned int)*2;
hex = new char[zahl];
decToHex();
}
void HexaInt::decToHex(){
sprintf(this->hex, "%x", this->decimal);
}
void HexaInt::hexToInt(){
sscanf(this->hex, "%x", &this->decimal);
}
void HexaInt::setDecimal(int d){
this->decimal = d;
decToHex();
}
int HexaInt::getDecimal()const{
return this->decimal;
}
HexaInt HexaInt::operator+(const HexaInt& other)const{
HexaInt result;
result.decimal = this->decimal+other.decimal;
sprintf(result.hex, "%x", result.decimal);
return result;
}
HexaInt HexaInt::operator-(const HexaInt& other)const{
HexaInt result;
result.decimal = this->decimal-other.decimal;
sprintf(result.hex, "%x", result.decimal);
return result;
}
HexaInt HexaInt::operator*(const HexaInt& other)const{
HexaInt result;
result.decimal = this->decimal*other.decimal;
sprintf(result.hex,"%x", result.decimal);
return result;
}
HexaInt HexaInt::operator/(const HexaInt& other)const{
HexaInt result;
result.decimal = this->decimal/other.decimal;
sprintf(result.hex,"%x",result.decimal);
return result;
}
HexaInt HexaInt::operator%(const HexaInt& other)const{
HexaInt result;
result.decimal = this->decimal%other.decimal;
sprintf(result.hex,"%x",result.decimal);
return result;
}
ostream& operator<<(ostream& s, const HexaInt& h){
cout << "DEZ: " << h.decimal <<" HEX: " << h.hex << endl;
return s;
}
istream& operator>>(istream& s, HexaInt& h){
int newValue;
s >> newValue;
h.setDecimal(newValue);
return s;
}
char& HexaInt::operator[](int i){
return this->hex[i];
}
HexaInt& HexaInt::operator=(const HexaInt& other){
if(&other != this){
delete[] hex;
this->decimal = other.decimal;
int zahl = sizeof(unsigned int)*2;
this->hex = new char[zahl];
decToHex();
}
return *this;
}
main.cpp
#include "HexaInt.h"
int main(){
HexaInt hex1(42);
cout << "Hex1-> " << hex1;
HexaInt hex2(hex1);
hex1 = hex2;
cout << "Hex2-> " << hex2;
cout << "Hex1 "<< hex1[0] << endl;
cout << "Hex2 "<< hex2[0] << endl;
hex1.setDecimal(18);
cout << "Hex1-> " << hex1;
cout << "Hex2-> " << hex2;
hex2.setDecimal(237);
cout << "Hex1-> " << hex1;
cout << "Hex2-> " << hex2;
HexaInt hex3 = hex1 + hex2;
cout << "Hex3-> " << hex3;
HexaInt a;
a[0] = 'F';
cout << "a-> " << a;
HexaInt b(a);
cout << "b-> " << b;
return 0;
}
结果是:
Hex1-> DEZ: 42 HEX: 2a
Hex2-> DEZ: 42 HEX: 2a
Hex1 2
Hex2 2
Hex1-> DEZ: 18 HEX: 12
Hex2-> DEZ: 42 HEX: 2a
Hex1-> DEZ: 18 HEX: 12
Hex2-> DEZ: 237 HEX: ed
Hex3-> DEZ: 255 HEX: ff
a-> DEZ: 0 HEX: F
b-> DEZ: 0 HEX: 0
最后的两个转换无法进行,谢谢! 求助?十六进制 求助 进制转换
[解决办法]
HexaInt a; //a是用HexaInt(int decimal=0);造的,所以decimal=0;
a[0] = 'F'; //使用char& HexaInt::operator[](int i){ return this->hex[i];} 的,
//但是decimal的值是 0;造成了 hex 和decimal 不一致。
cout << "a-> " << a;
HexaInt b(a); //b是用HexaInt(const HexaInt& copy);造的,所以b的decimal也0
cout << "b-> " << b;
建改:
加一hex造函.
HexaInt(const char* hexstr=0)
{
int zahl = sizeof(unsigned int)*2;
//空有少,因有'\0'束符,所以多加
zahl += 4;
hex = new char[zahl];
strcpy(hex,hexstr);
//要hex 字符 decimal, 有。
decimal = 0;
const char *phex = hexstr;
while (*phex){
decimal += hextodec(*phex);
decimal *= 16;
}
}
int hextodec(char h)
{
if ('0'<=ch && ch <='9')
return ch-'0';
if ('a'<=ch && ch <='f')
return 10+(ch-'a');
if ('A'<=ch && ch <='F')
return 10+(ch-'A');
return -1;
}
最後,就可以造a
HexaInt a("F");
以上代,提供思路,不保正.
[解决办法]
strtol
_ltoa