-
-
Notifications
You must be signed in to change notification settings - Fork 486
Expand file tree
/
Copy pathbinary_to_octal.dart
More file actions
54 lines (48 loc) · 1.36 KB
/
binary_to_octal.dart
File metadata and controls
54 lines (48 loc) · 1.36 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
47
48
49
50
51
52
53
54
import 'package:test/test.dart';
//Binary number to octal number conversion
void main() {
test("binary_to_octal -1111", () {
expect(binaryToOctal("-1111"), equals("-17"));
});
test("binary_to_octal 101011", () {
expect(binaryToOctal("101011"), equals("53"));
});
test("binary_to_octal rasies error when number is invalid", () {
expect(() => binaryToOctal("-1011a01"), throwsFormatException);
});
test("binary_to_octal of empty string raises error", () {
expect(() => binaryToOctal(""), throwsFormatException);
});
}
String binaryToOctal(String binaryString) {
binaryString = binaryString.trim();
if (binaryString.isEmpty) {
throw new FormatException("An empty value was passed to the function");
}
bool isNegative = binaryString[0] == "-";
if (isNegative) binaryString = binaryString.substring(1);
String octalValue = "";
int binary;
try {
binary = int.parse(binaryString);
} catch (e) {
throw new FormatException("An invalid value was passed to the function");
}
int currentBit;
int j = 1;
while (binary > 0) {
int code3 = 0;
for (int i = 0; i < 3; i++) {
currentBit = binary % 10;
binary = binary ~/ 10;
code3 += currentBit * j;
j *= 2;
}
octalValue = code3.toString() + octalValue;
j = 1;
}
if (isNegative) {
return "-" + octalValue;
}
return octalValue;
}