SatanasHammer
SatanasHammer

Reputation: 13

How to make a hex to decimal converter without using the functions for it in c++?

So I need to make a hex to decimal converter where the user decides how many numbers he want's to put in (e.g. if he put's in 3 the program let's him write 3 hexadecimal numbers which are converted to decimal) but as I said before I have to do the conversion manually and I can only use libraries iostream, cstring and cmath. And I can't use string (when the user puts in the hexadecimal numbers they can't be string (don't really know how to explain this) so for example the hex numbers will be stored in char hex and not string hex) If you could help me I would be really grateful and I hope I described the problem good enough!

This is my try (I am new to programming so it is pretty bad)

#include <iostream>
#include <cstring>
#include <cmath>
using namespace std;

int main()
{
    int broj;
    do
    {
        cout << "Unesite broj brojeva: ";
        cin >> broj;
    }
    while (broj < 1);
    char *hex = new char [broj];
    for (int i = 0; i < broj; i++)
    {
        cout << "Unesite " << i+1 << ". broj: ";
        cin >> hex[i];
    }
    for (int i = 0; i < broj; i++)
    {
        char idk[10000];
        idk[10000] = hex[i];
        int duljina = strlen(idk);
        int pom = 0;
        int halp = duljina;
        for (int j = 0; i < duljina; i++)
        {
            if (idk[j] == 'A' || idk[j] == 'a') idk[j] = 10;
            if (idk[j] == 'B' || idk[j] == 'b') idk[j] = 11;
            if (idk[j] == 'C' || idk[j] == 'c') idk[j] = 12;
            if (idk[j] == 'D' || idk[j] == 'd') idk[j] = 13;
            if (idk[j] == 'E' || idk[j] == 'e') idk[j] = 14;
            if (idk[j] == 'F' || idk[j] == 'f') idk[j] = 15;
            pom += idk[j]*(pow(16, halp));
            halp--;
        }
        cout << pom << endl;
    }

    return 0;
}

It doesn't work.

Upvotes: 0

Views: 787

Answers (2)

Topological Sort
Topological Sort

Reputation: 2806

Since this is apparently homework, I don't want to just post the code for you. But I worked on this and found several problems. I suggest taking them in this order:

You have a loop for reading in all the strings. OK, but although the array of strings is dynamically allocated, each string has not been allocated. That risks error. What's the fix? One easy one is: get rid of that loop. Inside the last for-loop, read in the string you want to work on. Process it and show its output. Then do the next one.

If you really want to read them all in and then process them, just make each string not be dynamically allocated. This would do it:

using str = char [256];
str* hexes = new str[broj];

Using that idk array makes things complicated. How about this algorithm for printing a single hex string?

for each character in the hex string counting BACKWARDS from the last character
       pom *= 16; //multiply the number you've got so far by 16 -- like moving left a digit
       figure out what the new digit means
       add that digit to pom

This eliminates the need for the idk array, so you don't have to worry about reading too far in it

Upvotes: 0

Lajos Arpad
Lajos Arpad

Reputation: 76601

Always try to separate your concerns. I will not deal with input/output, I assume that you can handle that. So, let's implement a function that converts a char array of certain length into decimal:

int getDigit(char input) {
    switch (input) {
        case '0': return 0;
        case '1': return 1;
        case '2': return 2;
        case '3': return 3;
        case '4': return 4;
        case '5': return 5;
        case '6': return 6;
        case '7': return 7;
        case '8': return 8;
        case '9': return 9;
        case 'a':
        case 'A': return 10;
        case 'b':
        case 'B': return 11;
        case 'c':
        case 'C': return 12;
        case 'd':
        case 'D': return 13;
        case 'e':
        case 'E': return 14;
        case 'f':
        case 'F': return 15;
    }
    return 0;
}

long hex2Decimal(char input[], int length) {
    long output = 0;
    long digit = 1;
    int index = 0;
    for (index = length - 1; index >= 0; index--) {
        output += getDigit(input[index]) * digit;
        digit *= 16;
    }
    return output;
}

Note, that the number of digits this code can support is limited. For the sake of simplicity I will not cover the support for more advanced cases.

Upvotes: 3

Related Questions