-
Notifications
You must be signed in to change notification settings - Fork 3
/
Convert.cpp
149 lines (128 loc) · 3.42 KB
/
Convert.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include "Arduino.h"
#include "Convert.h"
Convert::Convert(){}
/**
1: Convert the string into char array
2: Use char array with cpp strtol and base
Reference: http://www.cplusplus.com/reference/cstdlib/strtol/
*/
long Convert::binaryToDecimal(String value){
int base = 2;
int length = value.length() +1;
char valueAsArray[length];
value.toCharArray(valueAsArray, length);
return strtol(valueAsArray, NULL, base);
}
/**
1: Convert the string into char array
2: Use char array with cpp strtol and base
Reference: http://www.cplusplus.com/reference/cstdlib/strtol/
*/
long Convert::hexaToDecimal(String value){
int base = 16;
int length = value.length() +1;
char valueAsArray[length];
value.toCharArray(valueAsArray, length);
return strtol(valueAsArray, NULL, base);
}
/**
1: Convert the string into char array
2: Use char array with cpp strtol and base
Reference: http://www.cplusplus.com/reference/cstdlib/strtol/
*/
long Convert::octalToDecimal(String value){
int base = 8;
int length = value.length() +1;
char valueAsArray[length];
value.toCharArray(valueAsArray, length);
return strtol(valueAsArray, NULL, base);
}
/**
Using the divide by 2 and remainder method to create a binary string
Reference: http://www.wikihow.com/Convert-from-Decimal-to-Binary
*/
String Convert::decimalToBinary(long value){
String result = "";
if(value == 0){
return "0";
}else{
while(value > 0){
if((value % 2) == 0){
result = "0" + result;
}else{
result = "1" + result;
}
value /= 2;
}
}
return result;
}
/**
1: Convert the decimal value into a binary String
2: Each 4 bit pattern is translated into hexadecimal
Reference: http://www.rapidtables.com/convert/number/how-binary-to-hex.htm
*/
String Convert::decimalToHexa(long value){
String valueAsBinary = decimalToBinary(value);
String valueAsHexa = "";
String tmp = "";
// Loop over the binary list from right to left
for(int i = valueAsBinary.length() ; i > 0; i--){
if(tmp.length() == 4){
valueAsHexa = fourBitsToHex(tmp) + valueAsHexa;
tmp = "";
}
tmp = valueAsBinary.substring(i, i-1) + tmp;
}
valueAsHexa = fourBitsToHex(tmp) + valueAsHexa;
return valueAsHexa;
}
/**
1: Convert the decimal value into a binary String
2: Each 3 bit pattern is translated into octal
Reference: http://www.wikihow.com/Convert-Binary-to-Octal-Number
*/
String Convert::decimalToOctal(long value){
String valueAsBinary = decimalToBinary(value);
String valueAsOctal = "";
String tmp = "";
// Loop over the binary list from right to left
for(int i = valueAsBinary.length() ; i > 0; i--){
if(tmp.length() == 3){
valueAsOctal = String(binaryToDecimal(tmp)) + valueAsOctal;
tmp = "";
}
tmp = valueAsBinary.substring(i, i-1) + tmp;
}
valueAsOctal = String(binaryToDecimal(tmp)) + valueAsOctal;
return valueAsOctal;
}
/**
This method will translate a 4 bit binary value into the correct hexadecimal didgit
*/
String Convert::fourBitsToHex(String value){
int decValue = binaryToDecimal(value);
switch(decValue){
case 10:
return "A";
break;
case 11:
return "B";
break;
case 12:
return "C";
break;
case 13:
return "D";
break;
case 14:
return "E";
break;
case 15:
return "F";
break;
default:
return String(decValue);
break;
}
}