-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaseConvert.cpp
More file actions
46 lines (36 loc) · 1.11 KB
/
BaseConvert.cpp
File metadata and controls
46 lines (36 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/**
BaseConvert - convert from one base to another. Binary, octal, hexadecimal, decimal, etc.
@author OASIS systems
**/
#include "BaseConvert.h"
#include <iostream>
#include <string>
BaseConvert::BaseConvert() {}
// additional method for itoa
char* BaseConvert::reverse(char* buffer, int i, int j) {
while (i < j) {
char t = buffer[i];
buffer[i++] = buffer[j];
buffer[j--] = t;
}
return buffer;
}
// your C++ compiler may already include this method
char* BaseConvert::itoa(int value, char* buffer, int base) {
if (base < 2 || base > 32) return NULL;
int n = abs(value);
int i = 0;
do {
int r = n % base;
buffer[i++] = (r < 10) ? (r + '0') : (r - 10 + 'A');
n /= base;
} while (n);
if (value < 0 && base == 10) buffer[i++] = '-';
buffer[i] = '\0';
return reverse(buffer, 0, i - 1);
}
// Base convertion simple algorithm
std::string BaseConvert::convert(std::string input_str, uint8_t input_base, uint8_t output_base) {
int i = std::stoi(input_str, nullptr, input_base);
return itoa(i, buffer, output_base);
}