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.– Bacco
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
– Maniero