-
Notifications
You must be signed in to change notification settings - Fork 0
/
oct_binary_conv.cpp
49 lines (42 loc) · 1.12 KB
/
oct_binary_conv.cpp
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
47
48
49
/**
An implementation of octal to binary conversion
oct_binary_conv.cpp
@author raph-son
*/
#include <iostream>
#include <string>
using namespace std;
/**
Map a char to another base conversion
@param char character to be mapped to another
@return string a collection of four char representing new base
*/
string oct_to_bin_map(char oct);
// Program's entry point
int main(int c, char* argv[]) {
if(c < 2) {
cout << "Usage: Type octal number after program's name" << endl;
return 1;
}
char* oct_value = argv[1];
string oct_result;
for(int i=0; i<strlen(oct_value); i++) {
oct_result.append(oct_to_bin_map(oct_value[i]));
}
cout << oct_result << endl;
return 0;
}
string oct_to_bin_map(char oct) {
// Map an octal character to four binary bits
switch(oct) {
case '0': return "000";
case '1': return "001";
case '2': return "010";
case '3': return "011";
case '4': return "100";
case '5': return "101";
case '6': return "110";
case '7': return "111";
default: return "";
}
}