/* * Constructor: std::string B64coder::DecimalToBinary(int number) * * Purpose: Converts a decimal number to binary * * Arguments: int number = a decimal number * * Returns: String of the binary representation of a decimal number */ std::string B64coder::DecimalToBinary(int number) { if (number == 0) return "0"; if (number == 1) return "1"; if ( number % 2 == 0 ) { return DecimalToBinary(number/2) + "0"; }else{ return DecimalToBinary(number/2) + "1"; } }
int main() { double num; printf("Give me a decimal...\n"); scanf("%lf", &num); printf("Integer part: %lf, fractional part: %f", floor(num), num - floor(num)); DecimalToBinary(num); printf("Give me a binary (5 digits)...\n"); char binstr[5]; scanf("%s", binstr); BinaryToDecimal(binstr); }
/* * Constructor: std::string B64coder::DecToBin6(std::vector<int> decimals) * * Purpose: Converts each int in the vector to 6 bit binary * * Arguments: std::vector<int> decimals * * Returns: std::string = concatenated string of each 6bit binary value */ std::string B64coder::DecToBin6(std::vector<int> decimals) { std::string binary6; std::string temp; for (int i = 0; i < decimals.size(); i++) { temp = DecimalToBinary(decimals[i]); while( temp.length() < 6 ) { temp = "0" + temp; } binary6 += temp; } return binary6; }
/* * Constructor: std::string B64coder::To8Binary(char* key) * * Purpose: Converts the data to decimal, then to an 8 bit binary string * * Arguments: char* key = starting address of a char array * * Returns: String of 8 bit binary */ std::string B64coder::To8Binary(char* key) { std::string binary8; std::string temp; char *ptr = key; int decimal; while(*ptr) { decimal = *ptr; temp = DecimalToBinary(decimal); while( temp.length() < 8 ) { temp = "0" + temp; } binary8 += temp; ptr++; } return binary8; }
void MATRIX::CreateParity(void) { MATRIX *binaryOfColumn; int binarySum; for (int j=0;j<width;j++) for (int i=0;i<length;i++) { binaryOfColumn = DecimalToBinary(i,length-1); binarySum = int(binaryOfColumn->SumOfRow(0)); if ( (binarySum%2) == 0 ) Set(i,j,0); else Set(i,j,1); delete binaryOfColumn; binaryOfColumn = NULL; } }