Conversation from hexa to int and toupper do not work

Asked

Viewed 69 times

2

I need help with this code because I believe three functions

however hexToInt function is not properly converting only works conversation if hexa is an integer type 19 and not A or 1D.

and the functions toUpper and tolower do not print a letter with accent

toUper it brings the data as follows below:

MY NAME IS C++ that is, it does not capitalize the accented 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;
}
  • The @diegofm already made the kindness to edit your question, but remember that whenever you have questions with code snippet, post everything in the question itself. To format as code, select the snippet, and hit control+K on the keyboard, or use the button { } to format. There is a preview under the posting field that serves for you to check if it is ok.

  • Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site

1 answer

1

We had to add 10 in these lines, because the A is worth 10 and not 0:

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

and

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

So the function stays 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 on IDEONE.

As for the accented letters, you need to specify the LOCALE in the calls.

For example, including the <locale> in their headers and doing something like that:

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 the tolower of <locale>.

  • thus you used the locale it even compiled but the error it returns is this: terminate called after Throwing an instance of 'Std::runtime_error' what(): locale:::_S_create_c_locale name not Valid Aborted (saved core image)

  • Precisely, Voce needs to see in the compiler documentation what are the available locales. pus "pt_PT" example, but this varies, not all locales are the same in all standard libs. For example, if the format is "Germany_german", there are cases where you can use the short format and the encoding "en_US.utf-8", for example. You need to look at the compiler/lib documentation used to know what to put in place of "pt_EN".

Browser other questions tagged

You are not signed in. Login or sign up in order to post.