hexadecimal: iteration 1
This commit is contained in:
parent
9c9d3a5380
commit
cb28e44c67
5 changed files with 192 additions and 0 deletions
24
cpp/hexadecimal/hexadecimal.cpp
Normal file
24
cpp/hexadecimal/hexadecimal.cpp
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
#include "hexadecimal.h"
|
||||
|
||||
namespace hexadecimal {
|
||||
|
||||
int convert(const std::string &input)
|
||||
{
|
||||
int result = 0;
|
||||
|
||||
for (auto ch : input) {
|
||||
|
||||
ch = tolower(ch);
|
||||
|
||||
if (!isxdigit(ch))
|
||||
return 0;
|
||||
|
||||
result *= 16;
|
||||
result += ch <= '9' ? ch - '0'
|
||||
: ch - 'a' + 10;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue