C_CPP/02_MENT/main_02_MENT.cpp

58 lines
1.1 KiB
C++
Raw Normal View History

2018-03-09 09:47:53 +01:00
// file: main_02_MENT.cpp
// THIS IS C++, use clang++
#include <iostream>
2018-03-16 18:03:32 +01:00
#include "../helpers/println.hpp"
2018-03-09 09:47:53 +01:00
struct PascalString{
int length; // number of chars used
char characters[256]; // chars of some character string
};
2018-03-16 18:03:32 +01:00
int hexDigitToInt(char hexDigit) {
int value = 0;
if(hexDigit > 47 && hexDigit < 58)
value = hexDigit - 48;
if(hexDigit > 96 && hexDigit < 103)
value = hexDigit - 97 + 10;
2018-03-09 09:47:53 +01:00
return value;
}
2018-03-16 18:03:32 +01:00
int hexStringToInt(PascalString binaryDigits) {
2018-03-20 12:16:03 +01:00
int returnValue = 0;
for(int i = 0; i <= binaryDigits.length; ++i) {
returnValue += binaryDigits.characters[i];
}
2018-03-09 09:47:53 +01:00
return returnValue;
}
2018-03-20 12:16:03 +01:00
void printPascalString(PascalString s) {
for(int i = 0; i <= s.length; i++) {
println(s.characters[i]);
}
}
2018-03-16 18:03:32 +01:00
int main(int argc, char** argv, char** envp) {
2018-03-09 09:47:53 +01:00
PascalString s = {3, '1', '0', '0'};
PascalString s2 = {4, 'f', 'f', 'f', 'f'};
println(hexStringToInt(s));
println(hexStringToInt(s2));
2018-03-16 18:03:32 +01:00
2018-03-20 12:16:03 +01:00
println(hexDigitToInt('d'));
2018-03-16 18:03:32 +01:00
println(hexDigitToInt('9'));
println(hexDigitToInt('2'));
2018-03-20 12:16:03 +01:00
printPascalString(s2);
2018-03-09 09:47:53 +01:00
return 0;
}