Skip to content

Commit

Permalink
more useful error message in hex_to_int
Browse files Browse the repository at this point in the history
  • Loading branch information
Kota Mori committed Dec 4, 2017
1 parent 0034fe5 commit c77f0c9
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/dechex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,15 @@ IntegerVector hex_to_int(std::string h, char sep)

IntegerVector out;
bool started = false;
int start = 0;
for (int i = 0; i < h.size(); i++)
{
int start;
if (h[i] == sep) {
if (!started) {
start = i + 1;
started = true;
} else {
int end = i;

int tmp = 0;
int base = 1;
for (int k = end-1; k >= start; k--)
Expand All @@ -64,8 +63,12 @@ IntegerVector hex_to_int(std::string h, char sep)
if (c >= '0' && c <= '9') n = c - '0';
else if (c >= 'A' && c <= 'F') n = c - 'A' + 10;
else if (c >= 'a' && c <= 'f') n = c - 'a' + 10;
else stop("invalid hex");

else {
std::string mess = "invalid hex found: ";
mess += c;
mess += ", around: " + h.substr(start, 10);
stop(mess);
}
tmp += base*n;
base *= 16;
}
Expand Down

0 comments on commit c77f0c9

Please sign in to comment.