-
Jan Trávníček authoredJan Trávníček authored
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
hexavigesimal.cpp 1014 B
/*
* hexavigesimal.cpp
*
* Created on: 19. 4. 2014
* Author: Tomas Pecka
*/
#include <hexavigesimal>
#include <stdexcept>
namespace ext {
unsigned fromBase26( std::string rep ) {
unsigned n = 0;
for(char repSymbol : rep ) {
unsigned remainder = repSymbol - 'A';
if(remainder > 26)
throw std::invalid_argument(rep);
n = n * 26 + remainder;
}
return n;
}
std::string toBase26( unsigned n ) {
std::string name;
do {
name += ( char )( n % 26 + 'A' );
n = n / 26;
} while (n > 0);
return std::string( name.rbegin( ), name.rend( ) );
}
unsigned bijectiveFromBase26 ( std::string rep ) {
unsigned n = 0;
for (char repSymbol : rep ) {
unsigned remainder = repSymbol - 'A';
if(remainder > 26)
throw std::invalid_argument(rep);
n = n * 26 + remainder + 1;
}
return n;
}
std::string bijectiveToBase26(unsigned n) {
std::string name;
while(n > 0) {
--n;
name += (char) (n % 26 + 'A');
n /= 26;
}
return std::string(name.rbegin(), name.rend());
}
} /* namespace ext */