首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

十六进制和十进制的转换

2013-01-23 
求助 十六进制和十进制的转换我做得一个题目要求能够自由转换十进制和十六进制,现在十进制到十六进制的转

求助 十六进制和十进制的转换
我做得一个题目要求能够自由转换十进制和十六进制,现在十进制到十六进制的转换没有问题,但是十六进制到十进制的转换不成功,有哪位大大能帮我看一下,究竟要在哪儿改正,问题在哪儿?

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

热点排行