MP_02 intToDual

This commit is contained in:
Johannes Theiner 2018-03-27 21:35:41 +02:00
parent 6b0d32e796
commit 81222a26f9
7 changed files with 19 additions and 6 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -23,13 +23,13 @@ return value;
} }
int hexStringToInt(PascalString binaryDigits) { int hexStringToInt(PascalString binaryDigits) {
int returnValue = 0; int returnValue = 0;
for(int i = 0; i <= binaryDigits.length; ++i) { for(int i = 0; i <= binaryDigits.length; ++i) {
returnValue += binaryDigits.characters[i]; returnValue += binaryDigits.characters[i];
} }
return returnValue; return returnValue;
} }
@ -40,11 +40,22 @@ void printPascalString(PascalString s) {
} }
} }
int intToDual(int n) { PascalString intToDual(int n) {
int i = std::to_string(n).length() * 4;
PascalString string = {i};
while(n >= 1) {
string.characters[i] = (n % 2) + '0';
n = n / 2;
i--;
}
return string;
} }
int main(int argc, char** argv, char** envp) { int main(int argc, char** argv, char** envp) {
PascalString s = {3, '1', '0', '0'}; PascalString s = {3, '1', '0', '0'};
PascalString s2 = {4, 'f', 'f', 'f', 'f'}; PascalString s2 = {4, 'f', 'f', 'f', 'f'};
println(hexStringToInt(s)); println(hexStringToInt(s));
@ -64,6 +75,8 @@ int main(int argc, char** argv, char** envp) {
println(controlRegister); println(controlRegister);
printPascalString(intToDual(41));
return 0; return 0;
} }

Binary file not shown.