E
E
Egor Nameles2018-09-27 10:28:45
C++ / C#
Egor Nameles, 2018-09-27 10:28:45

How can I implement the conversion from hexadecimal to octal in my class?

Hello. I have created a class which initially has a hex string entered. Also added another line. And we check if the strings are equal, then we output 1, if not, then 0. Then we add these strings and print their length. And you need to translate these lines into the octal system. The last one doesn't work for me. This can be done either through inheritance or not. In SI I know what can be translated like this:
int n;
cout << "Enter a hexadecimal number: ";
scanf("%x", &n);
printf("%s %o", "Octal equals ", n);
Making it all work doesn't work. Please tell me how to implement it.
Here is my code. The teacher said there was nothing to fix. Just make a translation.
String.h

#pragma once
#include <iostream>
#include <cstring>

using namespace std;

class String
{
protected:
  int Length;
  char* Str;
public:
  String();
  String(const char* ptr);
  String(const String& t);
  String& operator = (String& t);
  String& operator += (const String& t);
  bool operator == (const String& t) const;
  bool operator != (const String& t) const;
  bool is_empty() const;
  const char* getStr() const;
  int getLength() const;

  ostream & show(ostream & os) const;
  friend ostream & operator << (ostream & os, const String & s)
  {
    return s.show(os);
  }

  ~String();
};

String.cpp
#include "String.h"
#include <iostream>
#include <cstring>

using namespace std;

String::String()
{
  Str = 0;
  Length = 0;
}

String::String(const char* ptr)
  : Length(strlen(ptr)), Str(new char[Length + 1])
{
  strcpy(Str, ptr);
}

String::String(const String& t)
  : Length(strlen(t.Str)), Str(new char[Length + 1])
{
  strcpy(Str, t.Str);
}
String& String::operator = (String& t)
{
  swap(Length, t.Length);
  swap(Str, t.Str);
  return *this;
}

String& String::operator += (const String& t)
{
  int newLength = Length + t.Length;
  char *newStr = new char[newLength + 1];
  strcpy(newStr, Str);
  strcat(newStr, t.Str);
  delete[] Str;
  Str = newStr;
  Length = newLength;
  return *this;
}

bool String::operator == (const String& t) const
{
  return Length == t.Length && strcmp(Str, t.Str) == 0;
}

bool String::operator != (const String& t) const
{
  return !(operator == (t));
}

bool String::is_empty() const
{
  return Str == 0 || Str[0] == '\0';
}

const char* String::getStr() const
{
  return Str;
}

int String::getLength() const
{
  return Length;
}

ostream & String::show(ostream & os) const
{
  return os << "\"" << (Str ? Str : "") << "\"";
}

String::~String()
{
  Length = 0;
  delete[] Str;
  Str = 0;
}

main.cpp
#include "String.h"
#include <iostream>
#include <conio.h>
#include <cstring>

using namespace std;

int main()
{
  setlocale(LC_ALL, "Russian");

  String s("0 1 2 3 4 5 6 7 8 9 A B C D E F");
  String f("Qwertyy");
  cout << s << endl;
  cout << (s == f) << endl;
  s += f;
  cout << s << endl;

  _getch();
  return 0;
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vitaly, 2018-09-27
@vt4a2h

Easiest option (and super inefficient):

std::string decString("10");
  
std::stringstream buf;
buf << std::oct << std::stoi(decString) << std::endl;
  
std::string octString = buf.str();
std::cout << octString << std::endl;

In general, in order to convert a number from a decimal number, for example, to the octal system, you can write a simple loop for 4-5 lines.

P
Pavel, 2018-09-27
Yazovskikh @unepic

If the compiler allows, you can use itoa, if not - sprintf. Not C++ but will work.

std::string hexString = "0xBAADF00D";
char octString[MAX_BUF];
itoa(std::stoi(hexString, 0, 16 ), octString, 8);

I came up with an interesting bike here. Since the hexadecimal and octal number systems are based on a power of two, this makes life much easier for us (scheme here ). 12 binary digits can be written as three hexadecimal digits or as four octal digits. Thus, to convert a hexadecimal string to octal, you need to replace all triples of hexadecimal characters (from the end, start with zeros) to replace the corresponding fours of octal:
#include <cstring>
#include <iostream>

unsigned getHexDigit(char hexChar)
{
  if (hexChar >= '0' && hexChar <= '9')
  {
    return hexChar - '0';
  }
  if (hexChar >= 'A' && hexChar <= 'F')
  {
    return 10 + hexChar - 'A';
  }	
  if (hexChar >= 'a' && hexChar <= 'f')
  {
    return 10 + hexChar - 'a';
  }
  return 0;
}

void getOctQuartet(const char* hex, char* oct)
{
  unsigned number = getHexDigit(hex[0]) << 8 | getHexDigit(hex[1]) << 4 | getHexDigit(hex[2]);
  oct[0] = '0' + (number >> 9 & 07);
  oct[1] = '0' + (number >> 6 & 07);
  oct[2] = '0' + (number >> 3 & 07);
  oct[3] = '0' + (number & 07);
}

char* getOctString(const char* hexString)
{
  const size_t hexStrLen = strlen(hexString);
  const size_t prefixLen = hexStrLen % 3;
  char* octString = new char[(hexStrLen - prefixLen + 3) / 3 * 4 + 1];	
  char* pOct = octString;
  if (prefixLen > 0)
  {
    char hexPrefix[3] = { '0'
      , prefixLen == 2 ? hexString[0] : '0'
      , prefixLen == 2 ? hexString[1] : hexString[0] 
    };
    getOctQuartet(hexPrefix, octString);
    pOct += 4;
  }

  const char* pHex = hexString + prefixLen;
  while(pHex < hexString + hexStrLen)
  {
    getOctQuartet(pHex, pOct);
    pHex += 3;
    pOct += 4;
  }
  *pOct = '\0';

  return octString;
}

int main()
{
  char hexString[] = "123456789ABCDEF";
  char* octString = getOctString(hexString);
  std::cout << octString << std::endl;
  delete[] octString;
  return 0;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question