Conversion from hex to int and toupper does not work

2

I need help with this code because I have created three functions

But the hexToInt function is not converting correctly, it only works if the hex is an integer type 19 and not A or 1D.

and the toUpper and toLower functions do not print a letter with an accent

toUper it after the data as follows below:

MY NAME is C ++ that is, it does not print the capital letter the same is said for the toLower function below:

My name is c ++

The code is this below:

#include <iostream>
#include <sstream>
#include <cctype>
#include <cstring>
#include <cstdio>

std::string toLower(std::string s)
{
    for (unsigned int i = 0; i < s.size(); i++)
        s[i] = towlower(s[i]);
    return s;
}

std::string toUpper(std::string s)
{
    for (unsigned int i = 0; i < s.size(); i++)
        s[i] = towupper(s[i]);
    return s;
}

int hexToInt(std::string s)
{
    int res = 0;

    for (size_t i = 0; i < s.size(); i++) {
        int multiplier = 1;
        int exp = (s.size() - 1 - i);
        while (exp-- > 0)
            multiplier *= 16;
        int ch = s[i];
        if (ch >= '0' && ch <= '9')
            res += multiplier * (ch - '0');
        else if (ch >= 'a' && ch <= 'z')
            res += multiplier * (ch - 'a');
        else if (ch >= 'A' && ch <= 'Z')
            res += multiplier * (ch - 'A');
    }
    return res;

}

int main () {

  std::cout<<"\n\n\thexToInt{0}: "<<hexToInt("0");
  std::cout<<"\n\n\thexToInt{1}: "<<hexToInt("1");
  std::cout<<"\n\n\thexToInt{2}: "<<hexToInt("2");
  std::cout<<"\n\n\thexToInt{3}: "<<hexToInt("3");
  std::cout<<"\n\n\thexToInt{4}: "<<hexToInt("4");
  std::cout<<"\n\n\thexToInt{5}: "<<hexToInt("5");
  std::cout<<"\n\n\thexToInt{6}: "<<hexToInt("6");
  std::cout<<"\n\n\thexToInt{7}: "<<hexToInt("7");
  std::cout<<"\n\n\thexToInt{8}: "<<hexToInt("8");
  std::cout<<"\n\n\thexToInt{9}: "<<hexToInt("9");
  std::cout<<"\n\n\thexToInt{A}: "<<hexToInt("A");
  std::cout<<"\n\n\thexToInt{B}: "<<hexToInt("B");
  std::cout<<"\n\n\thexToInt{C}: "<<hexToInt("C");
  std::cout<<"\n\n\thexToInt{D}: "<<hexToInt("D");
  std::cout<<"\n\n\thexToInt{E}: "<<hexToInt("E");
  std::cout<<"\n\n\thexToInt{F}: "<<hexToInt("F");
  std::cout<<"\n\n\thexToInt{10}: "<<hexToInt("10");
  std::cout<<"\n\n\thexToInt{11}: "<<hexToInt("11");
  std::cout<<"\n\n\thexToInt{12}: "<<hexToInt("12");
  std::cout<<"\n\n\thexToInt{13}: "<<hexToInt("13");
  std::cout<<"\n\n\thexToInt{14}: "<<hexToInt("14");
  std::cout<<"\n\n\thexToInt{15}: "<<hexToInt("15");
  std::cout<<"\n\n\thexToInt{16}: "<<hexToInt("16");
  std::cout<<"\n\n\thexToInt{17}: "<<hexToInt("17");
  std::cout<<"\n\n\thexToInt{18}: "<<hexToInt("18");
  std::cout<<"\n\n\thexToInt{19}: "<<hexToInt("19");
  std::cout<<"\n\n\thexToInt{1A}: "<<hexToInt("1A");
  std::cout<<"\n\n\thexToInt{1B}: "<<hexToInt("1B");
  std::cout<<"\n\n\thexToInt{1C}: "<<hexToInt("1C");
  std::cout<<"\n\n\thexToInt{1D}: "<<hexToInt("1D");
  std::cout<<"\n\n\thexToInt{1E}: "<<hexToInt("1E");
  std::cout<<"\n\n\thexToInt{1F}: "<<hexToInt("1F");
  std::cout<<"\n\n\thexToInt{20}: "<<hexToInt("20");
  std::cout<<"\n\n\t"<<toUpper("meu nome é c++");
  std::cout<<"\n\n\t"<<toLower("MEU NOME É C++")<<"\n\n";
  return 0;
}
    
asked by anonymous 16.06.2016 / 14:31

1 answer

1

There were 10 missing in these lines, since A is 10 instead of 0:

res += multiplier * (ch - 'a' + 10);

and

res += multiplier * (ch - 'A' + 10);

So, the function looks like this:

int hexToInt(std::string s)
{
    int res = 0;

    for (size_t i = 0; i < s.size(); i++) {
        int multiplier = 1;
        int exp = (s.size() - 1 - i);
        while (exp-- > 0)
            multiplier *= 16;
        int ch = s[i];
        if (ch >= '0' && ch <= '9')
            res += multiplier * (ch - '0');
        else if (ch >= 'a' && ch <= 'z')
            res += multiplier * (ch - 'a' + 10);
        else if (ch >= 'A' && ch <= 'Z')
            res += multiplier * (ch - 'A' + 10);
    }
    return res;
}

See your code already fixed and working at IDEONE .

For accented letters, you need to specify LOCALE for calls.

For example, including <locale> in your headers and doing something like this:

std::string toLower(std::string s)
{
    std::locale loc("pt_PT"); // Aqui é o problema, depende do compilador
    for (unsigned int i = 0; i < s.size(); i++)
        s[i] = tolower(s[i], loc);
    return s;
}

Note that in this case tolower of <locale> is being used.

    
16.06.2016 / 14:37