My friend compiled this with Visual Studio, but my compiler has trouble.
So, here is the code, and please don't think I'm being selfish by giving you all of it:
#ifndef STRING2BHEADER_H
#define STRING2BHEADER_H
#include <iostream>
#include <memory>
using namespace std;
typedef unsigned char CHAR;
const int MAX_CAPACITY = 8;
class String2B
{
public:
enum Mode {DECIMAL, HEXADECIMAL};
String2B();
//~String2B();
String2B(char* source);
void set(char* source);
void setLength(int newLength);
int getLength();
int getCapacity();
CHAR& operator[](int index);
String2B& operator=(char* source);
friend ostream& operator<<(ostream& os, String2B& source);
ostream& write(ostream& os, Mode mode=DECIMAL);
istream& read (istream& is);
private:
CHAR stringData[MAX_CAPACITY];
int length;
char convertHexadeToChar(unsigned int x);
void copySource(char* source);
void zort();
};
#endif //STRING2BHEADER_H
//and the .cpp file...
#include "String2BHeader.h"
String2B::String2B()
{
length=0;
}
String2B::String2B(char* source)
{
zort();
copySource(source);
}
void String2B::set(char* source)
{
copySource(source);
}
void String2B::zort()
{
length=0;
}
void String2B::copySource(char* source)
{
char* x = source;
while(*x)
{
++x;
}
length = x-source;
if (length > MAX_CAPACITY)
{
throw "don't do that";
}
memcpy(stringData, source, length);
}
void String2B::setLength(int newLength)
{
if (newLength<0 || newLength > MAX_CAPACITY)
{
throw "invalid length";
}
length = newLength;
}
int String2B::getLength()
{
return length;
}
int String2B::getCapacity()
{
return MAX_CAPACITY;
}
CHAR& String2B:perator[](int index)
{
if (index<0 || index>=length)
{
throw "index is bogus";
}
return stringData[index];
}
String2B& String2B:perator=(char* source)
{
copySource(source);
return *this;
}
ostream& String2B:perator<<(ostream& os, String2B& source)
{
if ((os.flags() & ios::basefield)==ios::hex)
{
String2B::Mode mode = String2B::HEXADECIMAL;
}
else
{
String2B::Mode mode = String2B:ECIMAL;
}
source.write(os, mode);
os.flush();
return os;
}
ostream& String2B::write(ostream& os, Mode mode)
{
int i;
switch(mode)
{
case DECIMAL:
for (i=0; i<length; i++)
{
os << stringData;
}
break;
case HEXADECIMAL:
CHAR left, right;
for (i=0; i<length; i++)
{
left = stringData >> 4;
right = stringData & 0xF;
os << convertHexadeToChar(left)
<< convertHexadeToChar(right);
//this was all one line
}
break;
}
return os;
}
istream& String2B::read(istream& is)
{
int i;
char c;
i = 0;
while(1)
{
is = is.get(c);
if (is.eof() || (int)c=='\n')
{
break;
}
if (i==MAX_CAPACITY)
{
throw "ERROR";
}
stringData[i++] = c;
}
length = i;
return is;
}
/*
ostream& String2B::writeHex(ostream& os)
{
}
*/
char String2B::convertHexadeToChar(unsigned int x)
{
if (x>15)
{
throw "bad hex val";
}
if (x<10)
{
return '0' + (char)x;
}
else
{
return 'A' + (char)(x-10);
}
}
So, here is the code, and please don't think I'm being selfish by giving you all of it:
#ifndef STRING2BHEADER_H
#define STRING2BHEADER_H
#include <iostream>
#include <memory>
using namespace std;
typedef unsigned char CHAR;
const int MAX_CAPACITY = 8;
class String2B
{
public:
enum Mode {DECIMAL, HEXADECIMAL};
String2B();
//~String2B();
String2B(char* source);
void set(char* source);
void setLength(int newLength);
int getLength();
int getCapacity();
CHAR& operator[](int index);
String2B& operator=(char* source);
friend ostream& operator<<(ostream& os, String2B& source);
ostream& write(ostream& os, Mode mode=DECIMAL);
istream& read (istream& is);
private:
CHAR stringData[MAX_CAPACITY];
int length;
char convertHexadeToChar(unsigned int x);
void copySource(char* source);
void zort();
};
#endif //STRING2BHEADER_H
//and the .cpp file...
#include "String2BHeader.h"
String2B::String2B()
{
length=0;
}
String2B::String2B(char* source)
{
zort();
copySource(source);
}
void String2B::set(char* source)
{
copySource(source);
}
void String2B::zort()
{
length=0;
}
void String2B::copySource(char* source)
{
char* x = source;
while(*x)
{
++x;
}
length = x-source;
if (length > MAX_CAPACITY)
{
throw "don't do that";
}
memcpy(stringData, source, length);
}
void String2B::setLength(int newLength)
{
if (newLength<0 || newLength > MAX_CAPACITY)
{
throw "invalid length";
}
length = newLength;
}
int String2B::getLength()
{
return length;
}
int String2B::getCapacity()
{
return MAX_CAPACITY;
}
CHAR& String2B:perator[](int index)
{
if (index<0 || index>=length)
{
throw "index is bogus";
}
return stringData[index];
}
String2B& String2B:perator=(char* source)
{
copySource(source);
return *this;
}
ostream& String2B:perator<<(ostream& os, String2B& source)
{
if ((os.flags() & ios::basefield)==ios::hex)
{
String2B::Mode mode = String2B::HEXADECIMAL;
}
else
{
String2B::Mode mode = String2B:ECIMAL;
}
source.write(os, mode);
os.flush();
return os;
}
ostream& String2B::write(ostream& os, Mode mode)
{
int i;
switch(mode)
{
case DECIMAL:
for (i=0; i<length; i++)
{
os << stringData;
}
break;
case HEXADECIMAL:
CHAR left, right;
for (i=0; i<length; i++)
{
left = stringData >> 4;
right = stringData & 0xF;
os << convertHexadeToChar(left)
<< convertHexadeToChar(right);
//this was all one line
}
break;
}
return os;
}
istream& String2B::read(istream& is)
{
int i;
char c;
i = 0;
while(1)
{
is = is.get(c);
if (is.eof() || (int)c=='\n')
{
break;
}
if (i==MAX_CAPACITY)
{
throw "ERROR";
}
stringData[i++] = c;
}
length = i;
return is;
}
/*
ostream& String2B::writeHex(ostream& os)
{
}
*/
char String2B::convertHexadeToChar(unsigned int x)
{
if (x>15)
{
throw "bad hex val";
}
if (x<10)
{
return '0' + (char)x;
}
else
{
return 'A' + (char)(x-10);
}
}